javascript技巧

1.从ES7开始,可以使用指数运算符**作为幂的简写,这比编写Math.pow(2, 3) 更快。 例如:2 ** 4 = 16
2.使用加法运算符+快速转为数字,使用~~也可以变为数字,但会不四舍五入的取整
3.位或运算符,可以用于从整数的末尾删除任意数量的数字。
console.log(1553 / 10 | 0) // Result: 155
console.log(1553 / 100 | 0) // Result: 15
console.log(1553 / 1000 | 0) // Result: 1
实际上,任何位运算符都会强制浮点数为整数,所以
console.log(23.9 | 0); // Result: 23
console.log(-23.9 | 0); // Result: -23

猜你喜欢

转载自www.cnblogs.com/wangxi01/p/11590116.html