午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    JavaScript的繼承
    來(lái)源:易賢網(wǎng) 閱讀:739 次 日期:2015-04-27 10:46:36
    溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript的繼承”,方便廣大網(wǎng)友查閱!

    prototype 屬性的作用:

    利用prototype 屬性提供對(duì)象的類(lèi)的一組基本功能。對(duì)象的新實(shí)例“繼承”賦予該對(duì)象原型的操作。

    prototype 屬性的功能:

    所有JavaScript 內(nèi)部對(duì)象都有只讀的prototype 屬性。可以為內(nèi)部對(duì)象的原型添加功能,但該對(duì)象不能被賦予不同的原型。

    然而,用戶(hù)定義的對(duì)象可以被賦給新的原型。

    constructor 屬性的作用:

    constructor 表示創(chuàng)建對(duì)象的函數(shù)。

    constructor 屬性的功能:

    constructor 屬性是所有具有 prototype 的對(duì)象的成員。它們包括除 Global 和 Math 對(duì)象以外的所有 JavaScript 內(nèi)部對(duì)象。

    constructor 屬性保存了對(duì)構(gòu)造特定對(duì)象實(shí)例的函數(shù)的引用。

    A 利用prototype 添加對(duì)象的屬性 [ 方式一]

    示例:

    <script type="text/javascript">

    //方式一

    var myObj = function(){

    this.study = "JavaScript";

    }

    myObj.prototype.hobby = function()

    {

    this.hobby = "See girl";

    }

    var newObj = new myObj();

    for ( var attr in newObj )

    {

    document.write( attr +"<br/>" );

    }

    </script>

    B 利用prototype 添加對(duì)象的屬性 [ 方式二]

    示例:

    <script type="text/javascript">

    //方式二

    var superObj = { name:"xugang" };

    var subObj = { age:20 };

    function extend(superObj,subObj){

    //獲得父對(duì)象的原型對(duì)象

    subObj.getSuper = superObj.prototype;

    //將父對(duì)象的屬性給子對(duì)象

    for(var i in superObj){

    subObj[i] = superObj[i];

    }

    }

    extend(superObj,subObj);

    for ( var s in subObj )

    {

    document.write( s +"<br/>" ); //遍歷子對(duì)象的屬性

    }

    </script>

    C 利用prototype 繼承父類(lèi)的原型屬性

    示例:

    <script>

    function Person(_name){

    this.name = _name;

    }

    //創(chuàng)建對(duì)象(用于更改 prototype 原型對(duì)象)

    function addSex(_sex){

    this.sex = _sex;

    }

    //更改原型對(duì)象

    Person.prototype = new addSex('男');

    var p = new Person('xugang');

    alert("p 的原型為:" + p.constructor);

    //打印所有屬性

    for(var i in p){

    //alert(p[i]);

    }

    // ================= 繼承 =================

    //創(chuàng)建子對(duì)象 Student

    function Student(_study){

    this.study = _study;

    }

    // 通過(guò) prototype 讓 Student 繼承 Person

    Student.prototype = new Person('劉德華');

    var stu1 = new Student('JS');

    alert("stu1 的原型為:" + stu1.constructor);

    for(var i in stu1){

    alert(stu1[i]);

    }

    </script>

    因?yàn)镾tudent 對(duì)象的原型更改為Person 對(duì)象,而Person 對(duì)象的原型更改為addSex ,所以,Student 對(duì)象的原型為addSex 。

    注意:一個(gè)對(duì)象的原型是在 new 對(duì)象的那一刻確定的,如果在 new 對(duì)象以后更改無(wú)效!

    D 如何設(shè)置對(duì)象的原型對(duì)象和構(gòu)造函數(shù)

    示例:

    <script type="text/javascript">

    function B(){

    this.name = "劉德華";

    return "B方法";

    }

    function C(){

    this.age = 42;

    return "C方法";

    }

    B.prototype = new C();

    var b = new B();

    b.constructor = B; //重寫(xiě)b的構(gòu)造方法為B本身

    document.write("b 的構(gòu)造方法:");

    document.write(b.constructor() + "<br/>");

    document.write("b 的原型對(duì)象的構(gòu)造方法:");

    document.write(b.constructor.prototype.constructor() + "<br/>");

    for ( var m in b )

    {

    document.write("屬性:" + m );

    document.write(" 值:" + b[m] +"<br/>");

    }

    </script>

    結(jié)果如下:

    b 的構(gòu)造方法:B方法

    b 的原型對(duì)象的構(gòu)造方法:C方法

    屬性:age 值:42

    屬性:name 值:劉德華

    E 對(duì)象中用來(lái)保存原型的 __proto__ 變量

    示例:

    <script type="text/javascript">

    function myObject(){}

    var my = new myObject();

    //任何對(duì)象都會(huì)有一個(gè)隱藏的 __proto__ 變量用來(lái)保存原型

    document.write(my.__proto__ + "<br/>");

    //在 Internet Explorer 下顯示為:undefined

    更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

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

    2025國(guó)考·省考課程試聽(tīng)報(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)系電話(huà):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)