JavaScript中8個(gè)常見的陷阱
這里我們針對(duì)JavaScript初學(xué)者給出一些技巧和列出一些陷阱。如果你已經(jīng)是一個(gè)磚家,也可以讀一讀。
1. 你是否嘗試過對(duì)數(shù)組元素進(jìn)行排序?JavaScript默認(rèn)使用字典序(alphanumeric)來排序。因此, [1,2,5,10].sort() 的結(jié)果是 [1, 10, 2, 5] 。
如果你想正確的排序,應(yīng)該這樣做: [1,2,5,10].sort((a, b) => a - b)
2. new Date() 十分好用new Date() 的使用方法有:
不接收任何參數(shù):返回當(dāng)前時(shí)間;接收一個(gè)參數(shù) x : 返回1970年1月1日 + x 毫秒的值。new Date(1, 1, 1) 返回1901年2月1號(hào)。然而...., new Date(2016, 1, 1) 不會(huì)在1900年的基礎(chǔ)上加2016,而只是表示2016年。3. 替換函數(shù)沒有真的替換?let s = 'bob' const replaced = s.replace(’b’, ’l’) replaced === 'lob' // 只會(huì)替換掉第一個(gè)b s === 'bob' // 并且s的值不會(huì)變
如果你想把所有的b都替換掉,要使用正則:
'bob'.replace(/b/g, ’l’) === ’lol’4. 謹(jǐn)慎對(duì)待比較運(yùn)算
// 這些可以 ’abc’ === ’abc’ // true 1 === 1 // true // 然而這些不行 [1,2,3] === [1,2,3] // false {a: 1} === {a: 1} // false {} === {} // false
因?yàn)閇1,2,3]和[1,2,3]是兩個(gè)不同的數(shù)組,只是它們的元素碰巧相同。因此,不能簡(jiǎn)單的通過 === 來判斷。
5. 數(shù)組不是基礎(chǔ)類型typeof {} === ’object’ // true typeof ’a’ === ’string’ // true typeof 1 === number // true // 但是.... typeof [] === ’object’ // true
如果要判斷一個(gè)變量 var 是否是數(shù)組,你需要使用 Array.isArray(var) 。
6. 閉包這是一個(gè)經(jīng)典的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,...,然而實(shí)際上卻不會(huì)。知道如何Debug嘛?
有兩種方法:
使用 let 而不是 var 。備注:可以參考Fundebug的另一篇博客 ES6之'let'能替代'var'嗎?使用 bind 函數(shù)。備注:可以參考Fundebug的另一篇博客 JavaScript初學(xué)者必看“this”Greeters.push(console.log.bind(null, i))當(dāng)然,還有很多解法。這兩種是我最喜歡的!7. 關(guān)于 bind
下面這段代碼會(huì)輸出什么結(jié)果?
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()
如果你說程序會(huì)崩潰,并且報(bào)錯(cuò):Cannot read property ’name’ of undefined。
因?yàn)榈?6行的 geet 沒有在正確的環(huán)境下執(zhí)行。當(dāng)然,也有很多方法解決這個(gè)BUG!
我喜歡使用 bind 函數(shù)來解決問題:asyncGreet () { this.someThingAsync() .then(this.greet.bind(this))}這樣會(huì)確保 greet 會(huì)被Foo的實(shí)例調(diào)用,而不是局部的函數(shù)的 this 。如果你想要 greet 永遠(yuǎn)不會(huì)綁定到錯(cuò)誤的作用域,你可以在構(gòu)造函數(shù)里面使用 bind 來綁 。
class Foo { constructor (name) { this.name = name this.greet = this.greet.bind(this) }}你也可以使用箭頭函數(shù)(=>)來防止作用域被修改。備注:可以參考Fundebug的另一篇博客 JavaScript初學(xué)者必看“箭頭函數(shù)” 。
asyncGreet () { this.someThingAsync() .then(() => { this.greet() })}8. Math.min()比Math.max()大
Math.min() < Math.max() // false
因?yàn)镸ath.min() 返回 Infinity, 而 Math.max()返回 -Infinity。
歡迎加入我們Fundebug的 全棧BUG監(jiān)控交流群: 622902485 。
來自:https://blog.fundebug.com/2017/06/28/who-said-js-was-easy/
相關(guān)文章:
1. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲2. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))3. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解4. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息5. HTML5 Canvas繪制圖形從入門到精通6. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法7. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式8. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能9. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明
