JavaScript對(duì)每個(gè)創(chuàng)建的對(duì)象都會(huì)設(shè)置一個(gè)原型,指向它的原型對(duì)象马僻。
當(dāng)我們用obj.xxx
訪問一個(gè)對(duì)象的屬性時(shí)肢娘,JavaScript引擎先在當(dāng)前對(duì)象上查找該屬性墓阀,如果沒有找到荡灾,就到其原型對(duì)象上找瓤狐,如果還沒有找到,就一直上溯到Object.prototype
對(duì)象批幌,最后础锐,如果還沒有找到,就只能返回undefined
荧缘。
例如皆警,創(chuàng)建一個(gè)Array
對(duì)象:
var arr = [1, 2, 3];
其原型鏈?zhǔn)牵?/p>
arr ----> Array.prototype ----> Object.prototype ----> null
Array.prototype
定義了indexOf()
、shift()
等方法截粗,因此你可以在所有的Array
對(duì)象上直接調(diào)用這些方法信姓。
當(dāng)我們創(chuàng)建一個(gè)函數(shù)時(shí):
function foo() {
return 0;
}
函數(shù)也是一個(gè)對(duì)象鸵隧,它的原型鏈?zhǔn)牵?/p>
foo ----> Function.prototype ----> Object.prototype ----> null
由于Function.prototype
定義了apply()
等方法,因此意推,所有函數(shù)都可以調(diào)用apply()
方法豆瘫。
很容易想到,如果原型鏈很長左痢,那么訪問一個(gè)對(duì)象的屬性就會(huì)因?yàn)榛ǜ嗟臅r(shí)間查找而變得更慢靡羡,因此要注意不要把原型鏈搞得太長系洛。
構(gòu)造函數(shù)
除了直接用{ ... }
創(chuàng)建一個(gè)對(duì)象外俊性,JavaScript還可以用一種構(gòu)造函數(shù)的方法來創(chuàng)建對(duì)象。它的用法是描扯,先定義一個(gè)構(gòu)造函數(shù):
function Student(name) {
this.name = name;
this.hello = function () {
alert('Hello, ' + this.name + '!');
}
}
你會(huì)問定页,咦,這不是一個(gè)普通函數(shù)嗎绽诚?
這確實(shí)是一個(gè)普通函數(shù)典徊,但是在JavaScript中,可以用關(guān)鍵字new
來調(diào)用這個(gè)函數(shù)恩够,并返回一個(gè)對(duì)象:
var xiaoming = new Student('小明');
xiaoming.name; // '小明'
xiaoming.hello(); // Hello, 小明!
注意卒落,如果不寫new
,這就是一個(gè)普通函數(shù)蜂桶,它返回undefined
儡毕。但是,如果寫了new
扑媚,它就變成了一個(gè)構(gòu)造函數(shù)腰湾,它綁定的this
指向新創(chuàng)建的對(duì)象,并默認(rèn)返回this
疆股,也就是說费坊,不需要在最后寫return this;
。
新創(chuàng)建的xiaoming
的原型鏈?zhǔn)牵?/p>
xiaoming ----> Student.prototype ----> Object.prototype ----> null
也就是說旬痹,xiaoming
的原型指向函數(shù)Student
的原型附井。如果你又創(chuàng)建了xiaohong
、xiaojun
两残,那么這些對(duì)象的原型與xiaoming
是一樣的:
xiaoming ↘
xiaohong -→ Student.prototype ----> Object.prototype ----> null
xiaojun ↗
用new Student()
創(chuàng)建的對(duì)象還從原型上獲得了一個(gè)constructor
屬性羡忘,它指向函數(shù)Student
本身:
xiaoming.constructor === Student.prototype.constructor; // true
Student.prototype.constructor === Student; // true
Object.getPrototypeOf(xiaoming) === Student.prototype; // true
xiaoming instanceof Student; // true
看暈了吧?用一張圖來表示這些亂七八糟的關(guān)系就是:
紅色箭頭是原型鏈磕昼。注意卷雕,Student.prototype
指向的對(duì)象就是xiaoming
、xiaohong
的原型對(duì)象票从,這個(gè)原型對(duì)象自己還有個(gè)屬性constructor
漫雕,指向Student
函數(shù)本身滨嘱。
另外,函數(shù)Student
恰好有個(gè)屬性prototype
指向xiaoming
浸间、xiaohong
的原型對(duì)象太雨,但是xiaoming
、xiaohong
這些對(duì)象可沒有prototype
這個(gè)屬性魁蒜,不過可以用__proto__
這個(gè)非標(biāo)準(zhǔn)用法來查看囊扳。
現(xiàn)在我們就認(rèn)為xiaoming
、xiaohong
這些對(duì)象“繼承”自Student
兜看。
不過還有一個(gè)小問題锥咸,注意觀察:
xiaoming.name; // '小明'
xiaohong.name; // '小紅'
xiaoming.hello; // function: Student.hello()
xiaohong.hello; // function: Student.hello()
xiaoming.hello === xiaohong.hello; // false
xiaoming
和xiaohong
各自的name
不同,這是對(duì)的细移,否則我們無法區(qū)分誰是誰了搏予。
xiaoming
和xiaohong
各自的hello
是一個(gè)函數(shù),但它們是兩個(gè)不同的函數(shù)弧轧,雖然函數(shù)名稱和代碼都是相同的雪侥!
如果我們通過new Student()
創(chuàng)建了很多對(duì)象,這些對(duì)象的hello
函數(shù)實(shí)際上只需要共享同一個(gè)函數(shù)就可以了精绎,這樣可以節(jié)省很多內(nèi)存速缨。
要讓創(chuàng)建的對(duì)象共享一個(gè)hello
函數(shù),根據(jù)對(duì)象的屬性查找原則代乃,我們只要把hello
函數(shù)移動(dòng)到xiaoming
旬牲、xiaohong
這些對(duì)象共同的原型上就可以了,也就是Student.prototype
:
修改代碼如下:
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
xiaoming.hello === xiaohong.hello; // true
用new
創(chuàng)建基于原型的JavaScript的對(duì)象就是這么簡單襟己!
忘記寫new怎么辦
如果一個(gè)函數(shù)被定義為用于創(chuàng)建對(duì)象的構(gòu)造函數(shù)引谜,但是調(diào)用時(shí)忘記了寫new
怎么辦?
在strict模式下擎浴,this.name = name
將報(bào)錯(cuò)员咽,因?yàn)?code>this綁定為undefined
,在非strict模式下贮预,this.name = name
不報(bào)錯(cuò)贝室,因?yàn)?code>this綁定為window
,于是無意間創(chuàng)建了全局變量name
仿吞,并且返回undefined
滑频,這個(gè)結(jié)果更糟糕。
所以唤冈,調(diào)用構(gòu)造函數(shù)千萬不要忘記寫new
峡迷。為了區(qū)分普通函數(shù)和構(gòu)造函數(shù),按照約定,構(gòu)造函數(shù)首字母應(yīng)當(dāng)大寫绘搞,而普通函數(shù)首字母應(yīng)當(dāng)小寫彤避,這樣,一些語法檢查工具如jslint將可以幫你檢測到漏寫的new
夯辖。
最后琉预,我們還可以編寫一個(gè)createStudent()
函數(shù),在內(nèi)部封裝所有的new
操作蒿褂。一個(gè)常用的編程模式像這樣:
function Student(props) {
this.name = props.name || '匿名'; // 默認(rèn)值為'匿名'
this.grade = props.grade || 1; // 默認(rèn)值為1
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
function createStudent(props) {
return new Student(props || {})
}
這個(gè)createStudent()
函數(shù)有幾個(gè)巨大的優(yōu)點(diǎn):一是不需要new
來調(diào)用圆米,二是參數(shù)非常靈活,可以不傳啄栓,也可以這么傳:
var xiaoming = createStudent({
name: '小明'
});
xiaoming.grade; // 1
如果創(chuàng)建的對(duì)象有很多屬性娄帖,我們只需要傳遞需要的某些屬性,剩下的屬性可以用默認(rèn)值谴供。由于參數(shù)是一個(gè)Object块茁,我們無需記憶參數(shù)的順序齿坷。如果恰好從JSON
拿到了一個(gè)對(duì)象桂肌,就可以直接創(chuàng)建出xiaoming
。