茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    深入淺析JavaScript中的constructor
    來(lái)源:易賢網(wǎng) 閱讀:822 次 日期:2016-07-08 09:57:35
    溫馨提示:易賢網(wǎng)小編為您整理了“深入淺析JavaScript中的constructor”,方便廣大網(wǎng)友查閱!

    constructor 屬性返回對(duì)創(chuàng)建此對(duì)象的數(shù)組函數(shù)的引用。本文給大家介紹JavaScript中的constructor ,需要的朋友參考下吧

    定義和用法

    constructor 屬性返回對(duì)創(chuàng)建此對(duì)象的數(shù)組函數(shù)的引用。

    語(yǔ)法

    object.constructor

    constructor,構(gòu)造函數(shù),對(duì)這個(gè)名字,我們都不陌生,constructor始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)。

    這里有一點(diǎn)需要注意的是,每個(gè)函數(shù)都有一個(gè)prototype屬性,這個(gè)prototype的constructor指向這個(gè)函數(shù),這個(gè)時(shí)候我們修改這個(gè)函數(shù)的prototype時(shí),就發(fā)生了意外。如

    function Person(name,age){

    this.name = name;

    this.age = age;

    }

    Person.prototype.getAge = function(){

    return this.age;

    }

    Person.prototype.getName = function(){

    return this.name;

    }

    var p = new Person("Nicholas",18);

    console.log(p.constructor); //Person(name, age)

    console.log(p.getAge()); //18

    console.log(p.getName()); //Nicholas

    但是如果是這樣:

    function Person(name,age){

    this.name = name;

    this.age = age;

    }

    Person.prototype = {

    getName:function(){

    return this.name;

    },

    getAge:function(){

    return this.age;

    }

    }

    var p = new Person("Nicholas",18);

    console.log(p.constructor); //Object()

    console.log(p.getAge()); //18

    console.log(p.getName()); //Nicholas

    結(jié)果constructor變了。

    原因就是prototype本身也是對(duì)象,上面的代碼等價(jià)于

    Person.prototype = new Object({

    getName:function(){

    return this.name;

    },

    getAge:function(){

    return this.age;

    }

    });

    因?yàn)閏onstructor始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù),那么就不難理解上面代碼p.constructor輸出的是Object了。

    對(duì)于修改了prototype之后的constructor還想讓它指向Person怎么辦呢?簡(jiǎn)單,直接給Person.prototype.constructor賦值就可以了:

    Person.prototype = {

    constructor:Person,

    getName:function(){

    return this.name;

    },

    getAge:function(){

    return this.age;

    }

    }

    以上所述是小編給大家介紹的JavaScript中的constructor ,希望對(duì)大家有所幫助!

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:深入淺析JavaScript中的constructor
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類(lèi)型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專(zhuān)用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
    咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)