800 字
4 分钟
JS零碎知识点整理

关于JS的零碎知识点整理#

#

1. 保留两位小数#

四舍五入#
var num = 2.446242342
num = num.toFixed(2) // 输出是字符串 需要用parseFloat或Number再转一下
不四舍五入#
Math.floor(15.7784514000 * 100) / 100 // 15.77

2. 字符串反转#

利用数组的reverse方法

let str = 'abcdefg'
let str_r1 = str.split('').reverse().join('') // 'gfedcba'
let str_r2 = [...str].reverse().join('') // 'gfedcba'

3. 一位数左边补0#

('0'+9).slice(-2) // '09'
('0'+12).slice(-2) // '12'
(''+9).padStart(2,0) // '09'
(''+12).padSart(2,0) // '12'

4. Object.is()判断两个值是否相等#

isNaN(NaN) // true
Object.is(NaN,NaN) // true
Object.is(1,1) // true

5. 字符与ASCII码互转#

String.fromCharCode(97) // 'a'
String.fromCharCode(65) // 'A'
'a'.charCodeAt() // 97
'abc'.charCodeAt(1) // 98

6. Object.getOwnPropertyDescriptors()#

ES5 的Object.getOwnPropertyDescriptor()方法会返回某个对象属性的描述(descriptor)。 ES8 引入了Object.getOwnPropertyDescriptors()方法,返回指定对象所有自身属性(非继承属性)的描述对象。

7.Object.getOwnPropertyDescriptors() 实现深拷贝#

该方法的引入目的,主要是为了解决Object.assign()无法正确拷贝get属性和set属性的问题

8. 右移一位可以实现除以2并向下取整#

5 >> 1 // 2
4 >> 1 // 2
9 >> 1 // 4
10 >> 1 // 5

9. typeof#

typeof 对初始化(未赋值)的变量返回undefined 对未声明的变量也会返回undefined ,而不是报错

var message
typeof message // undefined
typeof message2 // undefined

10. isFinite() 判断一个数值是否有穷#

正或负的infinity不能参与计算

isFinite(3) // true
isFinite(Infinity) // false

11. isNaN()的用法#

接受一个参数,会先将这个值转为数值,如果不能转为数字,直接返回true

isNaN(true) // false
isNaN('abc') // true
isNaN(10) // false
isNaN('10') // false
isNaN(null) // false
isNaN(undefined) // true

12. (123).toString() 与 ‘123’.toString()#

(123.toString())Number.prototype.toString 可用于数字转为各种进制

(45).toString(2) // "101101"
(45).toString(8) // "55"
(45).toString(16) // "2d"
(45).toString() // "45" 为空则默认为10进制

'123'.toString()String.prototype.toString

13. Array.from()生成值为0的二维数组#

// 错误的写法
arr = new Array(5).fill(new Array(5).fill(0))
// 如果这时只改变arr[0][1] 那么其他项的第一列也会跟着变换
arr[0][1] = 3
console.log(arr[1][1]) // 3
// 正确的写法
arr = Array.from(Array(5),()=>Array(5).fill(0))
arr[0][1] = 3
console.log(arr[1][1]) // 0

14 码点与字符串互换#

String.prototype.charCodeAt() 返回 0 到 65535 之间的整数(十进制),表示给定索引处的 UTF-16 代码单元 String.fromCharCode() 返回由指定的UTF-16代码单元序列创建的字符串

'z'.charCodeAt() // 122
String.fromCharCode('122','0o172','0x7a') // "zzz"

但是对于超过两个字节的字符ES5无法正确转换

String.fromCharCode(0x20BB7) // "ஷ" 乱码 因为0x20BB7超过两个字节,无法正确解读

ES6的两个方法可以解决这一bug

String.codePointAt() 返回UTF-16的完整码点 String.fromCodePoint() 返回由指定的UTF-16代码单元序列创建的字符串

"🚀".codePointAt() // 128640
String.fromCodePoint(128640) // "🚀"
"𠮷".codePointAt() // 134071
String.fromCodePoint(0x20BB7,134071) // "𠮷𠮷"
// 对于小于两个字节的也完全适用
"a".codePointAt() // 97
String.fromCodePoint(97) // "a"
"z".codePointAt() // 122
String.fromCodePoint('122','0o172','0x7a') // "zzz"

另外一些bug,请参考javascript有个unicode的天坑

15 生成指定范围的随机数 随机整数#

Math.random // [0,1)
Math.random()*10 // [0,10)
Math.ceil(Math.random()*10) // [0,10] 为0的概率极小 向上取整
Math.floor(Math.random()*10) // [0,9]
Math.round(Math.random()) // 只能是0或者1 概率各一半
Math.round(Math.random()*10) // [0,10],其中获取最小值0和最大值10的几率少一半
所以希望生成[min,max]的随机数,公式如下:
// parseInt() 和 Math.floor()结果都是向下取整
parseInt(Math.random()*(max - min + 1) + min,10)
Math.floor(Math.random()*(max - min +1) + min)
JS零碎知识点整理
https://blog.cxyxiaoyu.top/posts/js零碎知识点整理/
作者
小宇
发布于
2020-05-24
许可协议
CC BY-NC-SA 4.0