你可以使用{...}
直接創(chuàng)建對(duì)象
也可以通過(guò)Function
關(guān)鍵詞構(gòu)造的函數(shù)來(lái)創(chuàng)建對(duì)象。
本篇內(nèi)容所指的函數(shù),均已將箭頭函數(shù)排除在外。因?yàn)榧^函數(shù)不能用來(lái)構(gòu)造函數(shù)。箭頭函數(shù)沒(méi)有綁定
this,arguments,super,new.target
。
- 使用
new
關(guān)鍵字來(lái)調(diào)用一個(gè)函數(shù),就是使用構(gòu)造函數(shù)創(chuàng)建對(duì)象了。如果不使用new
關(guān)鍵字,那就是一個(gè)普通的函數(shù); - 一般構(gòu)造函數(shù)名使用大寫;
定義一個(gè)構(gòu)造函數(shù)
function Student(name) {
this.name = name;
this.hello = function () {
alert('Hello, ' + this.name + '!');
}
}
長(zhǎng)的跟普通函數(shù)一樣,不使用new
關(guān)鍵詞調(diào)用,就是普通函數(shù),使用new
關(guān)鍵詞調(diào)用就是構(gòu)造函數(shù)了。
使用new
關(guān)鍵字調(diào)用函數(shù),它綁定的this
指向新創(chuàng)建的對(duì)象,并默認(rèn)返回this
,也就是說(shuō) 不需要再構(gòu)造函數(shù)最后寫 return this
使用構(gòu)造函數(shù)創(chuàng)建對(duì)象
var xiaoming = new Student('小明');
xiaoming.name; // '小明'
xiaoming.hello(); // Hello, 小明!
JavaScript創(chuàng)建的每一個(gè)對(duì)象都會(huì)設(shè)置一個(gè)原型,指向它的原型對(duì)象
當(dāng)我們使用
obj.xxx
訪問(wèn)對(duì)象屬性時(shí),JavaScript引擎現(xiàn)在當(dāng)前對(duì)象上查找該屬性,如果沒(méi)有找到,就在其原型對(duì)象上找,如果沒(méi)有找到就一直上溯到Object.prototype
對(duì)象,最后,如果還沒(méi)有找到,就只能返回undefined。
例如創(chuàng)建一個(gè)數(shù)組 var a = [1,2,3];
它的原型鏈如下
a -> Array.prototype -> Object.prototype -> null
因?yàn)樵贏rray.Prototype上定義了push方法,所以在可以使用 a.push()
在這里 xiaoming 的原型鏈就是:
xiaoming -> Student.prototype -> Object.prototype -> null
但是xiaoming
可沒(méi)有prototype屬性,不過(guò)可以用__proto__
非標(biāo)準(zhǔn)語(yǔ)法查看,
Student.prototype
指向的就是xiaoming
的原型對(duì)象,默認(rèn)是個(gè)Student
類型的空對(duì)象,這個(gè)原型對(duì)象還有一個(gè)屬性constructor
指向Student
函數(shù)本身
Student.prototype
中屬性 constructor
也就可以使用了 比如xiaoming
也就擁有了 constructor
屬性,其實(shí)就是Student.prototype.constructor
也就是 Student
函數(shù)本身。
因此 xiaoming.constructor === Student.prototype.constructor === Student
如果我們通過(guò)new Student()
創(chuàng)建了很多對(duì)象,這些對(duì)象的hello
函數(shù)實(shí)際上只需要共享同一個(gè)函數(shù)就可以了,這樣可以節(jié)省很多內(nèi)存。
根據(jù)對(duì)象的屬性查找原則,我們只要把hello
函數(shù)移動(dòng)到 這些對(duì)象的共同的原型上就可以了。
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
而如果你直接給prototype
賦值,那么就會(huì)一不小心改變 Student 的原型 比如:
Student.prototype = {
hello: function () {
alert('Hello, ' + this.name + '!');
},
play:function(){
alert('play with me');
}
}
上面的代碼是賦值操作,而不是在原來(lái)的prototype對(duì)上增加屬性值,
原來(lái)的 Student.prototype
對(duì)象是 Student 類型的 {}
其打印結(jié)果是 Student {}
它的constructor 是Student函數(shù)本身
而直接賦值的這個(gè) {...}
默認(rèn)是 Object
類型的對(duì)象,這個(gè)Object對(duì)象的 prototype.constructor
也就是自然指向了它的構(gòu)造函數(shù) [Function: Object]
所以,使用Student
創(chuàng)建對(duì)象所繼承的constructor
屬性 就不再是原來(lái)的 Student
函數(shù) 而是 Object
函數(shù)。
如果想讓constructor
依然指向Student
修改原型對(duì)象的constructor
的屬性就可以了。
解決方案就是在設(shè)置完prototype
之后,設(shè)置下constructor
Student.prototype = {
hello: function () {
alert('Hello, ' + this.name + '!');
},
play:function(){
alert('play with me');
}
};
Student.prototype.constructor = Student;
盡管可以這樣解決,你也發(fā)現(xiàn)了,Student.prototype的打印結(jié)果已經(jīng)不是Student(Student {}
)的類型了,而是默認(rèn)對(duì)象Object 的類型,然后又更改constructor(Student.prototype.constructor),不覺(jué)得有不一致的地方嗎?一個(gè)Student類型的原型指向了一個(gè)Object類型的對(duì)象,其constructor卻是Student函數(shù)。
所以我不推薦使用這種方法 給原型對(duì)象 增加 屬性或者方法。雖然沒(méi)有改變?cè)玩湥歉淖兞嗽偷闹赶颍圆蛔⒁饪赡軙?huì)有一些意外的問(wèn)題發(fā)生。
我依然推崇直接在原來(lái)的原型對(duì)象上修改:
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};