新聞中心
constructor屬性始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)。比如下面例子:

專(zhuān)業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來(lái)客戶和效益!創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計(jì)制作,服務(wù)好的網(wǎng)站設(shè)計(jì)公司,做網(wǎng)站、網(wǎng)站制作負(fù)責(zé)任的成都網(wǎng)站制作公司!
- // 等價(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
- // 將上面兩段代碼合起來(lái),就得到下面的結(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
為什么呢?
原來(lái)是因?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
怎么修正這種問(wèn)題呢?方法也很簡(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
【編輯推薦】
- JavaScript類(lèi)和繼承:prototype屬性
- JavaScript類(lèi)和繼承:this屬性
- ExtJS Grid Tooltip的3+1種實(shí)現(xiàn)方式總結(jié)
- JavaScript異步調(diào)用框架的鏈?zhǔn)綄?shí)現(xiàn)
- JavaScript異步調(diào)用框架的jQuery風(fēng)格鏈?zhǔn)秸{(diào)用
本文題目:JavaScript類(lèi)和繼承:constructor屬性
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/cdjjeoi.html


咨詢(xún)
建站咨詢(xún)
