欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

JavaScript中的constructor屬性怎么使用

本篇內(nèi)容介紹了“JavaScript中的constructor屬性怎么使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

10年積累的網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有九江免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

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   // 將上面兩段代碼合起來(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中的constructor屬性怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章題目:JavaScript中的constructor屬性怎么使用
鏈接地址:http://www.chinadenli.net/article26/jdhsjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供GoogleApp設(shè)計(jì)服務(wù)器托管網(wǎng)站排名網(wǎng)站收錄小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)