天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

JavaScript類和繼承 constructor屬性

constructor屬性始終指向創建當前對象的構造函數。比如下面例子:比如下面例子:
復制代碼 代碼如下:
// 等價于 var foo = new Array(1, 56, 34, 12);
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等價于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由構造函數實例化一個obj對象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 將上面兩段代碼合起來,就得到下面的結論
console.log(obj.constructor.constructor === Function); // true

但是當constructor遇到prototype時,有趣的事情就發生了。
我們知道每個函數都有一個默認的屬性prototype,而這個prototype的constructor默認指向這個函數。如下例所示:
復制代碼 代碼如下:
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
// 將上兩行代碼合并就得到如下結果
console.log(p.constructor.prototype.constructor === Person); // true

當時當我們重新定義函數的prototype時(注意:和上例的區別,這里不是修改而是覆蓋),constructor屬性的行為就有點奇怪了,如下示例:
復制代碼 代碼如下:
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

為什么呢?
原來是因為覆蓋Person.prototype時,等價于進行如下代碼操作:
復制代碼 代碼如下:
Person.prototype = new Object({
getName: function() {
return this.name;
}
});

而constructor屬性始終指向創建自身的構造函數,所以此時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

怎么修正這種問題呢?方法也很簡單,重新覆蓋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

JavaScript技術JavaScript類和繼承 constructor屬性,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产色婷亚洲99精品AV | 掀开奶罩边躁狠狠躁软学生 | 国产精品第1页 | 欧美国产成人在线 | 久久中文字幕乱码免费 | 日本邪恶全彩工囗囗番海贼王 | 欧美亚洲曰韩一本道 | 兽交白浆喷水高潮 | 国产人妻精品久久久久久很牛 | 久久99精品国产麻豆婷婷 | 久久99精品国产麻豆婷婷 | 情浓野战台湾三级 | 亚洲伊人网站 | 啊…嗯啊好深男男小黄文 | 99久久精品毛片免费播放 | 久久99亚洲热最新地址获取 | 男女一边摸一边做羞羞的事情免费 | 最近中文字幕MV高清在线视频 | 久久水蜜桃亚洲AV无码精品偷窥 | 妈妈的朋友5在线观看免费完整版中文 | ass亚洲熟妇毛耸耸pics | 国产亚洲精品久久孕妇呦呦你懂 | 久久伊人免费 | 欧美大片免费观看 | 老师小扫货水能么多叫出来 | 亚洲高清国产品国语在线观看 | 思思99精品国产自在现线 | 欧美成人无码视频午夜福利 | 国产极品白嫩超清在线观看 | 少妇第一次交换 | 国产精品无码无卡毛片不卡视 | 岛国大片在线观看免费版 | 亚洲国产精品特色大片观看 | QVOD理论 | 国偷自产视频一区二区久 | 视频成人永久免费视频 | 幼儿交1300部一区二区 | 快播性爱电影 | 男助理憋尿PLAY灌尿BL出去 | 亚洲一区乱码电影在线 | 超h高h肉h文教室生理课 |