JS——10大陷阱

一、警惕A>X>B写法

  3>2>1  返回值为false(原因:3>2为true,会默认转成数字1,1>1为false)

  1<4<3  返回值为true(原因:1<4为true,会默认转成数字1,1<3为true)

  2<1<2  返回值为true(原因:2<1为false,会默认转成数字0,0<2为true)

处理办法:这类写法拆开单独判断

 案例:   1>X&&X>3         这样可以避免首次判断后直接把true或者false转为1或者0。

二、你是否尝试过对数组元素进行排序?

JavaScript默认使用字典序(alphanumeric)来排序。因此, [1,2,5,10].sort()的结果是[1, 10, 2, 5]。

如果你想正确的排序,应该这样做: [1,2,5,10].sort((a, b) => a - b)

三、new Date() 十分好用

new Date()可以接收:

  • - 不接收任何参数:返回当前时间;
  • - 接收一个参数`x`: 返回1970年1月1日 + `x`毫秒的值。
  • - `new Date(1, 1, 1)`返回1901年2月1号。
  • - 然而...., `new Date(2016, 1, 1)`不会在1900年的基础上加2016,而只是表示2016年。

四、替换函数没有真的替换?

1

2

3

4

let s = "bob"

const replaced = s.replace('b', 'l')

replaced === "lob" // 只会替换掉第一个b

s === "bob" // 并且s的值不会变

如果你想把所有的b都替换掉,要使用正则:

1

"bob".replace(/b/g, 'l') === 'lol'

五、谨慎对待比较运算

1

2

3

4

5

6

7

// 这些可以

'abc' === 'abc' // true

1 === 1 // true

// 然而这些不行

[1,2,3] === [1,2,3] // false

{a: 1} === {a: 1} // false

{} === {} // false

因为[1,2,3]和[1,2,3]是两个不同的数组,只是它们的元素碰巧相同。因此,不能简单的通过`===`来判断。

六、数组不是基础类型

1

2

3

4

5

typeof {} === 'object' // true

typeof 'a' === 'string' // true

typeof 1 === number // true

// 但是....

typeof [] === 'object' // true

如果要判断一个变量`var`是否是数组,你需要使用`Array.isArray(var)` 。

七、闭包

这是一个经典的JavaScript面试题:

1

2

3

4

5

6

7

const Greeters = []

for (var i = 0 ; i < 10 ; i++) {

 Greeters.push(function () { return console.log(i) })

}

Greeters[0]() // 10

Greeters[1]() // 10

Greeters[2]() // 10

虽然期望输出0,1,2,...,然而实际上却不会。知道如何Debug嘛?

有两种方法:

1

Greeters.push(console.log.bind(null, i))

当然,还有很多解法。这两种是我最喜欢的!

八、关于bind

下面这段代码会输出什么结果?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

class Foo {

 constructor (name) {

 this.name = name

 }

 greet () {

 console.log('hello, this is ', this.name)

 }

 someThingAsync () {

 return Promise.resolve()

 }

 asyncGreet () {

 this.someThingAsync()

 .then(this.greet)

 }

}

new Foo('dog').asyncGreet()

如果你说程序会崩溃,并且报错:Cannot read property 'name' of undefined 。

因为第16行的`geet`没有在正确的环境下执行。当然,也有很多方法解决这个BUG!

- 我喜欢使用`bind`函数来解决问题:

1

2

3

4

asyncGreet () {

 this.someThingAsync()

 .then(this.greet.bind(this))

}

这样会确保`greet`会被Foo的实例调用,而不是局部的函数的`this`。

- 如果你想要`greet`永远不会绑定到错误的作用域,你可以在构造函数里面使用`bind`来绑定。

1

2

3

4

5

6

class Foo {

 constructor (name) {

 this.name = name

 this.greet = this.greet.bind(this)

 }

}

- 你也可以使用箭头函数(=>)来防止作用域被修改。 (备注:可以参考这篇文章 JavaScript箭头(arrow)函数详解_基础知识_脚本之家)  

1

2

3

4

5

6

asyncGreet () {

 this.someThingAsync()

 .then(() => {

 this.greet()

 })

}

九、Math.min()比Math.max()大

1

Math.min() < Math.max() // false

十、new Date().getMonth();

console.log(new Date());
console.log(new Date().getMonth());
console.log(new Date().getMonth()+1);

如上运行结果说明:new Date().getMonth()方法返回的是上一月,取当前月份必须new Date().getMonth()+1。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科技颠覆未来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值