《JavaScript啟示錄》摘抄

來源:互聯網
上載者:User

標籤:

1、JavaScript預封裝的9個原生的物件建構函數:

Number(),String(),Boolean(),Object(),Array(),Function(),Data(),RegExp(),Error()

2、以上對象返回的類型除了Function返回function外,其他的都返回object。

var myString=new String(‘foo‘);            console.log(typeof myString);            var myRegExp=new RegExp(‘\\bt[a-z]+\\b‘);            console.log(typeof myRegExp);            var myFunction=new Function("x","y","return x+y");            console.log(typeof myFunction);            var myNumber=new Number(23);            console.log(typeof myNumber);            var myObject=new Object();            console.log(typeof myObject);            var myArray=new Array(‘foo‘,‘bar‘);            console.log(typeof myArray);            var myData=new Date();            console.log(typeof myData);            var myError=new Error(‘Crap!‘);            console.log(typeof myError);            var myBoolean=new Boolean(false);            console.log(typeof myBoolean);

3、原生對象、自訂物件建構函數  代碼對比

var Person=funtion(living,age,gender)            {                this.living=living;                this.age=age;                this.gender=gender;                this.getGender(){return this.gender;}            }            var codyA=new Person(true,33,‘male‘);//自訂建構函式            var codyB=new Object;            codyB.living=true;    //原生對象

4、複雜值:原生物件建構函數Object(),Array(),Function(),Data(),RegExp(),Error()是複雜類型,因為他們包含一個或多個原始值或複雜值。      P18

5、原始值(簡單值)複製的時候覆制的是值,複雜值複製的時候是複製的地址,即按照引用的方式複製。

6、使用new關鍵字建立String(),Number(),Boolean()值時,或者這些值在後台被轉換成複雜物件時,即使原始值可以被看做是複雜值,他們也不具備通過引用進行複雜計算的特性。區別於第五點。     P20

7、複雜物件比較是按照地址比較的,即如果兩個比較的對象指向同一地址,那麼輸出true,否則輸出false,這與簡單值的比較按照值比較不同。還需要注意的是,複雜物件的比較符號是===,注意是三個=。

8、delete是刪除對象屬性的唯一方法,因為將對象的屬性設定為undefined或者null時只能改變屬性的值,而不會將該屬性從對象中刪除。注意,delete不會刪除原型鏈上找到的屬性。

var foo={bar:‘shabi‘};            delete foo.bar;            console.log(‘bar‘ in foo);

結果會輸出false,因為foo對象的bar屬性已經被刪除了。

9、hasOwnProperty函數可以檢測一個屬性是否來自原型鏈,當來自對象本身時,返回true,當來自原型鏈時返回false。

var myTest={foo:‘value‘};            console.log(myTest.hasOwnProperty(‘foo‘));    //返回true            console.log(myTest.hasOwnProperty(‘toString‘));    //返回false

10、區別於第9點,當我們使用   in  操作符尋找某個屬性的時候,他不光會尋找自己本身的屬性列表,還會通過原型鏈尋找,而一旦找到了這個屬性,都會返回true。     P37

var myTest={foo:‘value‘};            console.log(myTest.hasOwnProperty(‘foo‘));    //返回true            console.log(myTest.hasOwnProperty(‘toString‘));    //返回false            console.log(‘toString‘ in myTest);            //返回true

11、for in迴圈不僅會遍曆特定對象的區域屬性,還會遍曆所繼承(通過原型鏈)的對象上的屬性,因此,我們需要在迴圈內加入hasOwnProperty()函數過濾掉哪些通過原型鏈尋找到的屬性。

var cody={age:23,gender:‘male‘};            for(var key in cody)            {                if(cody.hasOwnProperty(key))                {console.log(key);}        //輸出age和gender            }

12、JavaScript和c++的變數都是以字母、底線開頭的,以字母、數字、底線組成的字串來命名變數。

13、函數都會傳回值,當我們沒有顯式設定傳回值的時候,函數會返回undefined。     P52

14、自調用函數:在定義函數運算式後立即調用該函數運算式。       P59

var sayWord=function(){console.log(‘大帥B‘)}();

15、在JavaScript中,只有全域範圍、局部範圍(函數範圍)、evel範圍。它不同於c++,它不存在塊範圍,即在if、for語句塊內無法建立範圍,變數可以相互覆蓋。  P78

var temp=10;            //temp在任何地方都有效  包括test函數和eval語句            var test=function()            {    console.log(temp);                var temp1=100;        //temp1隻在test函數範圍內有效            }();            eval(‘var temp2=1000;console.log(temp);‘); //temp2隻在eval語句中有效            console.log(temp);            console.log(temp1);    //輸出undefined            console.log(temp2); //輸出undefined

16、JavaScript會將缺少var的變數聲明為全域變數,即便這個變數在函數體中。    P78

var temp=10;            //temp在任何地方都有效  包括test函數和eval語句            var test=function()            {    console.log(temp);                temp1=100;        //temp1前面沒有var  此時他是全域範圍的            }();            eval(‘temp2=1000;console.log(temp);‘); //temp2前面沒有var  此時他是全域範圍的            console.log(temp);            console.log(temp1);    //輸出100            console.log(temp2); //輸出1000

17、閉包:

var count=function()            {                var i=0;                return function()                {                    return i++;                }            }()        //這裡的()不要忘記 起到count函數立即調用自己的作用            console.log(count());    //輸出0            console.log(count());    //輸出1            console.log(count());    //輸出2

18、有沒有new關鍵字很重要:

var myBoolean=Boolean(0);    //這裡沒有使用new關鍵字  當做原始值處理            if(myBoolean)                console.log(‘myBoolean is true‘);            else                console.log(‘myBoolean is false‘);                            var myBoolean1=new Boolean(0);    //使用new關鍵字  false布爾對象 實際上是true對象            if(myBoolean1)                console.log(‘myBoolean1 is true‘);            else                console.log(‘myBoolean1 is false‘);

輸出結果:

可以看到沒有使用new關鍵字時,輸出false;

使用了new關鍵字時,輸出true。(非原始false布爾對象轉換為true)       P111

19、對於擁有null值的變數,typeof操作符會返回object。    P117

var myObject=null;            console.log(typeof myObject);            console.log(myObject===null);

輸出為:

《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.