JAVASCRIPT 簡單嗎

來源:互聯網
上載者:User

標籤:解決方案   字串   ons   placed   add   sync   code   範圍   上下文   

1. 你有沒有嘗試給一組數字排序?

 

Javascript 的sort()函數在預設情況下使用字母數字(字串Unicode碼點)排序。

 

所以[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 毫秒。 瞭解 Unix 的人知道為什麼。

  • new Date(1, 1, 1) 返回 1901, 二月 , 1號。因為,第一個參數表示1900年加1年,第二個參數表示這一年的第二個月(因此是二月)?—?腦迴路正常的人會從1開始索引?—?,第三個參數很明顯是這個月的第一天,所以1?—?有時候索引確實從1開始?—?。

  • new Date(2016, 1, 1) 不會給1900年加上2016。它僅代表2016年。

 

3. Replace 並不“替代”

let s = "bob"const replaced = s.replace(‘b‘, ‘l‘)replaced === "lob"s === "bob"

覺得這是一件好事,因為我不喜歡函數改變它們的輸入。 你還應該知道 replace 只會替換第一個匹配的字串:

 

如果你想替換所有匹配的字串,你可以使用帶/g標誌的Regex :

 

"bob".replace(/b/g, ‘l‘) === ‘lol‘ // 替換所有匹配的字串

4. 比較的時候要注意

// These are ok‘abc‘ === ‘abc‘ // true1 === 1         // true// These are not[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// But....typeof [] === ‘object‘  // true

如果你想知道你的變數是不是數組,你仍然可以用Array.isArray(myVar)

6. 閉包

 

這是一個很有名的面試題:

 

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… ? 你知道它為什麼不是這樣輸出的嗎? 你會怎樣修改讓它輸出 0, 1, 2… ?

 

這裡有兩種可能的解決方案:

 

用 let 替代 var. Boom. 解決了.

 let和var的不同在於範圍。var的範圍是最近的函數塊,let的範圍是最近的封閉塊,封閉塊可以小於函數塊(如果不在任何塊中,則let和var都是全域的)。

 

替代方法: 用 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 沒有在正確的上下文中運行。同樣,這個問題依然有很多解決方案。

 

我個人喜歡

 

asyncGreet() {

    this.someThingAsync()

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

}

 

這樣可以確保類的執行個體作為上下文調用greet。

 

如果你認為greet 不應該在執行個體上下文之外運行, 你可以在類的constructor中綁定它:

 

class Foo {

    constructor(name) {

        this.name = name

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

    }

}

 

你還應該知道箭頭函數( => )可以用來保留上下文。這個方法也可以:

 

asyncGreet() {

    this.someThingAsync()

        .then(() = > {

        this.greet()

    })

}

 

(本文來自轉載)

 

JAVASCRIPT 簡單嗎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.