你可以使用{...}
直接創(chuàng)建對象
也可以通過Function
關(guān)鍵詞構(gòu)造的函數(shù)來創(chuàng)建對象让腹。
本篇內(nèi)容所指的函數(shù),均已將箭頭函數(shù)排除在外领追。因為箭頭函數(shù)不能用來構(gòu)造函數(shù)笤闯。箭頭函數(shù)沒有綁定
this,arguments咆课,super,new.target
末誓。
- 使用
new
關(guān)鍵字來調(diào)用一個函數(shù),就是使用構(gòu)造函數(shù)創(chuàng)建對象了书蚪。如果不使用new
關(guān)鍵字喇澡,那就是一個普通的函數(shù); - 一般構(gòu)造函數(shù)名使用大寫;
定義一個構(gòu)造函數(shù)
function Student(name) {
this.name = name;
this.hello = function () {
alert('Hello, ' + this.name + '!');
}
}
長的跟普通函數(shù)一樣,不使用new
關(guān)鍵詞調(diào)用善炫,就是普通函數(shù)撩幽,使用new
關(guān)鍵詞調(diào)用就是構(gòu)造函數(shù)了。
使用new
關(guān)鍵字調(diào)用函數(shù)箩艺,它綁定的this
指向新創(chuàng)建的對象窜醉,并默認返回this
,也就是說 不需要再構(gòu)造函數(shù)最后寫 return this
使用構(gòu)造函數(shù)創(chuàng)建對象
var xiaoming = new Student('小明');
xiaoming.name; // '小明'
xiaoming.hello(); // Hello, 小明!
JavaScript創(chuàng)建的每一個對象都會設(shè)置一個原型艺谆,指向它的原型對象
當我們使用
obj.xxx
訪問對象屬性時榨惰,JavaScript引擎現(xiàn)在當前對象上查找該屬性,如果沒有找到静汤,就在其原型對象上找琅催,如果沒有找到就一直上溯到Object.prototype
對象居凶,最后,如果還沒有找到藤抡,就只能返回undefined侠碧。
例如創(chuàng)建一個數(shù)組 var a = [1,2,3];
它的原型鏈如下
a -> Array.prototype -> Object.prototype -> null
因為在Array.Prototype上定義了push方法,所以在可以使用 a.push()
在這里 xiaoming 的原型鏈就是:
xiaoming -> Student.prototype -> Object.prototype -> null
但是xiaoming
可沒有prototype屬性缠黍,不過可以用__proto__
非標準語法查看弄兜,
Student.prototype
指向的就是xiaoming
的原型對象,默認是個Student
類型的空對象,這個原型對象還有一個屬性constructor
指向Student
函數(shù)本身
Student.prototype
中屬性 constructor
也就可以使用了 比如xiaoming
也就擁有了 constructor
屬性瓷式,其實就是Student.prototype.constructor
也就是 Student
函數(shù)本身替饿。
因此 xiaoming.constructor === Student.prototype.constructor === Student
如果我們通過new Student()
創(chuàng)建了很多對象,這些對象的hello
函數(shù)實際上只需要共享同一個函數(shù)就可以了贸典,這樣可以節(jié)省很多內(nèi)存视卢。
根據(jù)對象的屬性查找原則,我們只要把hello
函數(shù)移動到 這些對象的共同的原型上就可以了廊驼。
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
而如果你直接給prototype
賦值据过,那么就會一不小心改變 Student 的原型 比如:
Student.prototype = {
hello: function () {
alert('Hello, ' + this.name + '!');
},
play:function(){
alert('play with me');
}
}
上面的代碼是賦值操作,而不是在原來的prototype對上增加屬性值蔬充,
原來的 Student.prototype
對象是 Student 類型的 {}
其打印結(jié)果是 Student {}
它的constructor 是Student函數(shù)本身
而直接賦值的這個 {...}
默認是 Object
類型的對象蝶俱,這個Object對象的 prototype.constructor
也就是自然指向了它的構(gòu)造函數(shù) [Function: Object]
所以班利,使用Student
創(chuàng)建對象所繼承的constructor
屬性 就不再是原來的 Student
函數(shù) 而是 Object
函數(shù)饥漫。
如果想讓constructor
依然指向Student
修改原型對象的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 {}
)的類型了闯割,而是默認對象Object 的類型彻消,然后又更改constructor(Student.prototype.constructor),不覺得有不一致的地方嗎宙拉?一個Student類型的原型指向了一個Object類型的對象宾尚,其constructor卻是Student函數(shù)。
所以我不推薦使用這種方法 給原型對象 增加 屬性或者方法谢澈。雖然沒有改變原型鏈煌贴,但是改變了原型的指向,稍不注意可能會有一些意外的問題發(fā)生锥忿。
我依然推崇直接在原來的原型對象上修改:
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};