1.原始值與引用值
原始值存放在棧里, 引用值存放在堆里. 如程序:
var num = 10;
var bol = true;
var str = "abc";
var obj = new Object();
var arr = ['a','b','c'];
var person = new Person(100,"笨蛋的座右銘",25);
2.undefined和null
undefined: 變量未定義; 是Undefined類型的專屬值;
null:引用未分配; 是Null類型的專屬值.
typeof(undefined) == undefined;
typeof(null) == object;
undefined==null;
undefined!==null;
null instanceof Object == false;
undefined instanceof Object == false;
雖然有Undefined和Null類型, 但是通過下面的例子說明這兩個類型是不可見的, 也就是說我們只能使用他們的值:
alert(undefined instanceof Undefined);
alert(null instanceof Null);
3.偽數(shù)組
特點:
1) 具有l(wèi)ength屬性;
2) 像數(shù)組一樣按索引順序存取數(shù)據(jù);
3) 不具備數(shù)組特有的操作數(shù)據(jù)的方法如push, pop, slice...
偽數(shù)組都可以通過Array.prototype.slice轉(zhuǎn)換為真正的數(shù)組:
var faceArray = {0: 'a', 1: 'b', length: 2}//標準的偽數(shù)組;
var realArray = Array.prototype.slice.call(fakeArray);
js中的偽數(shù)組:arguments,?。睿飀e.childNodes, document.getElementsByTagName()...
IE中的問題 : IE中node.childNodes是不能用slice轉(zhuǎn)化的.
Jquery中的偽數(shù)組 : Jquery本身就是一個偽數(shù)組:
alert($('.class1').length); alert($('.class1').[0].tagName);
4.關(guān)于簡單類型的字面量
var a = 1; b = true, c = "ccc";
字面量看起來有類型
alert(typeof a);//number
alert(typeof b);//boolean
alert(typeof c);//string
但是通過instanceof卻測不出來
alert(a instanceof Number)//false
alert(a instanceof Object)//false
alert(b instanceof Boolean)//false
alert(b instanceof Object)//false
alert(c instanceof String)//false
alert(c instanceof Object)//false
5.函數(shù)的prototype屬性和對象實例的內(nèi)部prototype屬性
每個function(構(gòu)造函數(shù))都有一個prototype屬性, 每個對象實例都有一個不可見的(mozilla把它公開了, 可以通過__proto__來取得)內(nèi)部的prototype屬性, 它指向構(gòu)造函數(shù)的prototype屬性. prototype還可以有它自己的prototype屬性, 這構(gòu)成了prototype鏈, Object是最頂?shù)膶ο? 所以所有的prototype鏈最終會指向Object.prototype. 當訪問對象實例的屬性/方法的時候, 從對象實例自己開始搜索, 若果搜索不到, 沿著prototype鏈向上搜索, 直到Object.prototype.prototype == null 為止.
6.構(gòu)造函數(shù)的一個小秘密
只要 new 表達式之后的 constructor 返回(return)一個引用對象(數(shù)組,對象,函數(shù)等),都將覆蓋new創(chuàng)建的匿名對象,如果返回(return)一個原始類型(無 return 時其實為 return 原始類型 undefined),那么就返回 new 創(chuàng)建的匿名對象.
7.對象的創(chuàng)建的過程
var p = new Person('zhangsan');
◦創(chuàng)建一個build-in object對象obj并初始化;
◦將p的內(nèi)部[[Prototype]]指向Person.prototype;
◦將p作為this,使用arguments參數(shù)調(diào)用Person的內(nèi)部[[Call]]方法, 即執(zhí)行Person函數(shù)體, 并返回返回值, 如果沒有return, 則返回undefined;
◦如果前一步返回的是Object類型, 則返回這個值給p, 否則返回obj.
8.對象的自有屬性和繼承屬性
9.函數(shù)對象的創(chuàng)建過程
創(chuàng)建一個build-in object對象fn;
將fn的內(nèi)部[[Prototype]]設為Function.prototype;
設置內(nèi)部的[[Call]]屬性,它是內(nèi)部實現(xiàn)的一個方法,處理函數(shù)調(diào)用的邏輯。(簡單的理解為指向函數(shù)體);
設置fn.length為funArgs.length,如果函數(shù)沒有參數(shù),則將fn.length設置為0;
fn.prototype的constructor指向fn自己;
返回fn.
10.instanceof的原理
查看a是不是B的實例, 就是看B的prototype(構(gòu)造函數(shù)的prototype屬性)指向的對象在不在a的原形鏈上.
11.關(guān)于Function和Object的猜測
alert(Function instanceof Function);//true
alert(Function instanceof Object);//true
alert(Object instanceof Function);//true
alert(Object instanceof Object);//true
想了好久, 沒有想透......
新聞熱點
疑難解答