<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                js是門靈活的語言,實現一種功能往往有多種做法,ECMAScript沒有明確的繼承機制,而是通過模仿實現的,根據js語言的本身的特性,js實現繼承有以下通用的幾種方式 1.使用對象冒充實現繼承(該種實現方式可以實現多繼承) 實現原理:讓父類的構造函數成為子類的方法,然后調用該子類的方法,通過this關鍵字給所有的屬性和方法賦值 ``` function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); //40 } } function Child(firstname) { this.parent=Parent; this.parent(firstname); delete this.parent; this.saySomeThing=function() { console.log(this.fname); //李 this.sayAge(); } } var mychild=new Child("李"); mychild.saySomeThing(); ``` 2.采用call方法改變函數上下文實現繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式) 實現原理:改變函數內部的函數上下文this,使它指向傳入函數的具體對象 ``` function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); //40 } } function Child(firstname) { this.saySomeThing=function() { console.log(this.fname); //張 this.sayAge(); } this.getName=function() { return firstname; } } var child=new Child("張"); Parent.call(child,child.getName()); child.saySomeThing(); ``` 3.采用Apply方法改變函數上下文實現繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式) 實現原理:改變函數內部的函數上下文this,使它指向傳入函數的具體對象 ``` function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); //40 } } function Child(firstname) { this.saySomeThing=function() { console.log(this.fname); //張 this.sayAge(); } this.getName=function() { return firstname; } } var child=new Child("張"); Parent.apply(child,[child.getName()]); child.saySomeThing(); ``` 4.采用原型鏈的方式實現繼承 實現原理:使子類原型對象指向父類的實例以實現繼承,即重寫類的原型,弊端是不能直接實現多繼承 ``` function Parent() { this.sayAge=function() { console.log(this.age); } } function Child(firstname) { this.fname=firstname; this.age=40; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } Child.prototype=new Parent(); var child=new Child("張"); child.saySomeThing(); ``` 5.采用混合模式實現繼承 ``` function Parent() { this.sayAge=function() { console.log(this.age); } } Parent.prototype.sayParent=function() { alert("this is parentmethod!!!"); } function Child(firstname) { Parent.call(this); this.fname=firstname; this.age=40; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } Child.prototype=new Parent(); var child=new Child("張"); child.saySomeThing(); child.sayParent(); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看