精通Javascript讀書筆記1

來源:互聯網
上載者:User

《精通Javascript》

Chap 1

1. 使用命名空間保護你的庫。

YAHOO.util.Event.addListner(...);

Chap 2

2. 引用

多個變數能夠指向同一對象。引用指向的只能是具體對象,而不是另一個引用;不允許多層引用。

但是,在執行字串串連操作時,結果總是一個新的字串對象,而非源字串的修改版本。

eg1.

var object=new Object();var objRef=obj;obj.oneProperty=true;alert(obj.oneProperty === objRef.oneProperty);

eg2.

var items=new Array("one","two","three");var itemsRef=items;items=new Array("new","Array");alert(items!=itemsRef);

eg3.

var item="test";var itemRef=item;item+="ing";alert(item!=itemRef);

 

3. 判斷傳入參數類型

如果沒有提供參數,類型必定為undefined。

if( typeof msg == 'undefined') {...}

1) typeof語句

當變數不是object或者array類型時,這個是一個很好的解決方案。但是對於自訂對象,比如user就不能用這個方法進行類型檢查,因為他只會返回object。

2) 建構函式

這一屬性引用原本用來構造對象的那個函數。

// check numberif(num.constructor == String)      num=parseInt(num); //check arrayif(str.constructor == Array)      str=str.join(',');

3) 對比 typeof 和 建構函式

變數 typeof變數 建構函式
{an:“object”} object Object
[“an”,”array”] object Array
function(){} function Function
“a string” string String
55 number Number
true boolean Boolean
new User() object User
相關文章

聯繫我們

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