JavaScript類和繼承 constructor屬性
更新時(shí)間:2010年03月04日 12:47:55 作者:
本文介紹了JavaScript里面的constructor屬性。這個(gè)屬性是理解JavaScript類和繼承的重要基礎(chǔ)。
constructor屬性始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)。比如下面例子:比如下面例子:
// 等價(jià)于 var foo = new Array(1, 56, 34, 12);
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等價(jià)于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由構(gòu)造函數(shù)實(shí)例化一個(gè)obj對(duì)象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 將上面兩段代碼合起來,就得到下面的結(jié)論
console.log(obj.constructor.constructor === Function); // true
但是當(dāng)constructor遇到prototype時(shí),有趣的事情就發(fā)生了。
我們知道每個(gè)函數(shù)都有一個(gè)默認(rèn)的屬性prototype,而這個(gè)prototype的constructor默認(rèn)指向這個(gè)函數(shù)。如下例所示:
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 將上兩行代碼合并就得到如下結(jié)果
console.log(p.constructor.prototype.constructor === Person); // true
當(dāng)時(shí)當(dāng)我們重新定義函數(shù)的prototype時(shí)(注意:和上例的區(qū)別,這里不是修改而是覆蓋),constructor屬性的行為就有點(diǎn)奇怪了,如下示例:
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
為什么呢?
原來是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
而constructor屬性始終指向創(chuàng)建自身的構(gòu)造函數(shù),所以此時(shí)Person.prototype.constructor === Object,即是:
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
怎么修正這種問題呢?方法也很簡(jiǎn)單,重新覆蓋Person.prototype.constructor即可:
function Person(name) {
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
復(fù)制代碼 代碼如下:
// 等價(jià)于 var foo = new Array(1, 56, 34, 12);
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等價(jià)于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由構(gòu)造函數(shù)實(shí)例化一個(gè)obj對(duì)象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 將上面兩段代碼合起來,就得到下面的結(jié)論
console.log(obj.constructor.constructor === Function); // true
但是當(dāng)constructor遇到prototype時(shí),有趣的事情就發(fā)生了。
我們知道每個(gè)函數(shù)都有一個(gè)默認(rèn)的屬性prototype,而這個(gè)prototype的constructor默認(rèn)指向這個(gè)函數(shù)。如下例所示:
復(fù)制代碼 代碼如下:
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 將上兩行代碼合并就得到如下結(jié)果
console.log(p.constructor.prototype.constructor === Person); // true
當(dāng)時(shí)當(dāng)我們重新定義函數(shù)的prototype時(shí)(注意:和上例的區(qū)別,這里不是修改而是覆蓋),constructor屬性的行為就有點(diǎn)奇怪了,如下示例:
復(fù)制代碼 代碼如下:
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
為什么呢?
原來是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:
復(fù)制代碼 代碼如下:
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
而constructor屬性始終指向創(chuàng)建自身的構(gòu)造函數(shù),所以此時(shí)Person.prototype.constructor === Object,即是:
復(fù)制代碼 代碼如下:
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
怎么修正這種問題呢?方法也很簡(jiǎn)單,重新覆蓋Person.prototype.constructor即可:
復(fù)制代碼 代碼如下:
function Person(name) {
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
相關(guān)文章
一實(shí)用的實(shí)現(xiàn)table排序的Javascript類庫
一實(shí)用的實(shí)現(xiàn)table排序的Javascript類庫...2007-09-09
Javascript面向?qū)ο笤O(shè)計(jì)一 工廠模式
工廠模式抽象了創(chuàng)建具體對(duì)象的過程,但是在ECMAScript中無法創(chuàng)建類,所以就使用一種函數(shù)來封裝以特定接口創(chuàng)建對(duì)象的細(xì)節(jié)2011-12-12
javascript 面向?qū)ο缶幊? function是方法(函數(shù))
在進(jìn)行編程時(shí),必免不了要碰到復(fù)雜的功能。初學(xué)者最怕復(fù)雜的功能,因?yàn)椴荒軌蚝芎玫倪M(jìn)行功能邊界劃分,只能一大串if、循環(huán)加case堆疊在一起,結(jié)果出來的程序自己看著暈,別人看著更暈。
2009-09-09
Javascript面向?qū)ο缶幊蹋ǘ?構(gòu)造函數(shù)的繼承
這個(gè)系列的第一部分,主要介紹了如何"封裝"數(shù)據(jù)和方法,以及如何從原型對(duì)象生成實(shí)例。
2011-08-08
js創(chuàng)建對(duì)象的幾種常用方式小結(jié)(推薦)
最近在看javascript高級(jí)程序設(shè)計(jì),其中對(duì)對(duì)象的創(chuàng)建做了具體的闡述,綜合起來,總結(jié)了下(je知識(shí)庫javascript專欄由這方面的教程,有興趣的可以去知識(shí)庫看看)
2010-10-10
Javascript面向?qū)ο缶幊蹋ㄈ?非構(gòu)造函數(shù)的繼承
這個(gè)系列的第一部分介紹了"封裝",第二部分介紹了使用構(gòu)造函數(shù)實(shí)現(xiàn)"繼承"。
2011-08-08 
