In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I would like to talk to you about the pits often encountered in the 8 JavaScript. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
JavaScript uses dictionary order (alphanumeric) to sort by default. Therefore, [1, 2, 5, 5, 10]. Sort () results in [1, 10, 2, 5]. If you want to sort correctly, you should do this: [1Jing 2jue 5je 10] .sort ((a, b) = > a-b)
1. Have you ever tried to sort array elements?
JavaScript uses dictionary order (alphanumeric) to sort by default. Therefore, [1, 2, 5, 5, 10]. Sort () results in [1, 10, 2, 5].
If you want to sort correctly, you should do this: [1Jing 2jue 5je 10] .sort ((a, b) = > a-b)
2. New Date () is very useful
New Date () can be used in the following ways:
Do not receive any parameters: return the current time
Receive a parameter x: returns the value of January 1, 1970 + x milliseconds.
New Date (1,1,1) returns to February 1, 1901.
However, new Date (2016, 1, 1) will not add 2016 to 1900, but only means 2016.
3. There is no real replacement for the replacement function?
Let s = "bob" const replaced = s.replace ('baked,' l') replaced = "lob" / / will only replace the first bs = "bob" / / and the value of s will not change
If you want to replace all the bs, use the regular:
"bob" .replace (/ bUnip g,'l') = = 'lol'
4. Be cautious about comparison operation
/ / these can be 'abc' = =' abc' / / true1 = 1 / / true//, but these can't [1meme 2jue 3] / / false {a: 1} = = {a: 1} / / false {} = {} / / false
Because [1BET 2p3] and [1JRO 2p3] are two different arrays, but their elements happen to be the same. Therefore, it cannot be judged simply by =.
5. Array is not a base type
Typeof {} = = 'object' / / truetypeof' a'= = 'string' / / truetypeof 1 = number / / true// but.... typeof [] = =' object' / / true
If you want to determine whether a variable var is an array, you need to use Array.isArray (var).
6. Closure
This is a classic JavaScript interview question:
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) } } 你也可以使用箭头函数(=>To prevent the scope from being modified Note: you can refer to another Fundebug blog JavaScript beginners must see "Arrowhead function".
AsyncGreet () {this.someThingAsync () .then (() = > {this.greet ()})}
8. Math.min () is larger than Math.max ()
Math.min () < Math.max () / / false
Because Math.min () returns Infinity, while Math.max () returns-Infinity.
After reading the above, do you have any further understanding of the pits commonly encountered in the 8 JavaScript? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.