ITPub博客

首页 > 应用开发 > Javascript > 好程序员分享JavaScript中8个常见的陷阱

好程序员分享JavaScript中8个常见的陷阱

原创 Javascript 作者:好程序员IT 时间:2019-04-10 17:46:00 0 删除 编辑

好程序员 分享 JavaScript 中 8 个常见的陷阱 , 这里我们针对 JavaScript 初学者给出一些技巧和列出一些陷阱。如果你已经是一个砖家,也可以读一读。

 

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

 

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

 

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

 

2. 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 年。

3. 替换函数没有真的替换?

 

let s = "bob"

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

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

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

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

 

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

4. 谨慎对待比较运算

 

// 这些可以

'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] 是两个不同的数组,只是它们的元素碰巧相同。因此,不能简单的通过 === 来判断。

 

5. 数组不是基础类型

 

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

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

typeof 1 === number // true

// 但是 ....

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

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

 

6. 闭包

 

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

 

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 嘛?

有两种方法:

 

使用 let 而不是 var 。备注:可以参考 Fundebug 的另一篇博客 ES6 之” let ”能替代” var ”吗 ?

使用 bind 函数。备注:可以参考 Fundebug 的另一篇博客 JavaScript 初学者必看“ this ”

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

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

 

7. 关于 bind

 

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

 

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 。

 

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

 

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

 

asyncGreet () {

this.someThingAsync()

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

}

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

 

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

 

class Foo {

    constructor(name) {

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

    }

}

3 、你也可以使用箭头函数 (=>) 来防止作用域被修改。备注:可以参考 Fundebug 的另一篇博客 JavaScript 初学者必看“箭头函数”。

 

asyncGreet() {

    this.someThingAsync().then(() = >{

        this.greet()

    })

}

8. Math.min() 比 Math.max() 大

 

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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69913892/viewspace-2640963/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
欢迎关注公众号:好程序员特训营 web前端教程分享 723729549 大数据教程分享 703503210

注册时间:2019-03-20

  • 博文量
    539
  • 访问量
    410779