8个JavaScript中常遇到的坑是怎样的

发布时间:2021-09-30 18:01:29 作者:柒染
来源:亿速云 阅读:116

今天就跟大家聊聊有关8个JavaScript中常遇到的坑是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

JavaScript默认使用字典序(alphanumeric)来排序。因此, [1,2,5,10].sort() 的结果是 [1, 10, 2, 5] 。 如果你想正确的排序,应该这样做: [1,2,5,10].sort((a, b) => a - b)

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" // 只会替换掉第一个bs === "bob" // 并且s的值不会变

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

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

4. 谨慎对待比较运算

// 这些可以'abc' === 'abc' // true1 === 1         // true// 然而这些不行 [1,2,3] === [1,2,3] // false{a: 1} === {a: 1}   // false{} === {}           // false

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

5. 数组不是基础类型

typeof {} === 'object'  // truetypeof 'a' === 'string' // truetypeof 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]() // 10Greeters[1]() // 10Greeters[2]() // 10

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

a.使用let而不是var。

b.使用bind函数。

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。 因为greet没有在正确的环境下执行。当然,也有很多方法解决这个BUG!

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

asyncGreet () {  this.someThingAsync()  .then(this.greet.bind(this)) }

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

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

class Foo {  constructor (name) {    this.name = name    this.greet = this.greet.bind(this)  } }

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

asyncGreet () {  this.someThingAsync()  .then(() => {    this.greet()  }) }

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

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

因为Math.min() 返回 Infinity, 而 Math.max()返回 -Infinity。

看完上述内容,你们对8个JavaScript中常遇到的坑是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. hadoop遇到的坑
  2. addView遇到的坑及其解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript

上一篇:2017年10个最好的Javascript和CSS库分别是怎样的

下一篇:老司机常用的几个JavaScript调试技巧是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》