Js 類型方面的神坑

來源:互聯網
上載者:User

標籤:

你有沒有遇見過本來好好的一個數組結果 typeof 出來是個 object 的情況,你有沒有遇到過非要寫個 typeof x === undefined 的情況...

人生呀,就是這麼豐富多彩,喜怒無常!

 

先來看幾個好玩的

typeof new String("x");   // object"x" == new String("x");   // true

一臉懵逼,明明一個是 object 一個是 string,怎麼就 true 了。

當然 === 的時候就是 false 了,所以周遭才會推薦使用 === 而不是 == 的說

 

[] == new Array();    // false{} == new Object();   // false

等等,結果和類型都一樣,怎麼和上面的又不一樣了呢,二臉懵逼

 

 

var x;                             // undefinedx == undefined;             // truex == null;                      // truetypeof x;                        // undefinedtypeof x == undefined;  // false

  

 

Js 類型方面的神坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.