<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之旅 廣告
                ## 原型繼承 ``` function People() {} People.prototype.name="小明"; People.prototype.age = 20; People.prototype.say = function() { console.log("haha"); } function Student(){} Student.prototype = new People(); var student= new Student(); console.log(student.age); ``` ## 原型繼承的優缺點 優點: 1. 實現簡單,可實現屬性和方法的共享 缺點: 1. 創建子類實例時,無法向父類構造函數傳參 2. 屬性包含引用類型時,更改子類會將所有的子類都修改 ## 構造函數繼承 ``` function People(name, age) { this.name = name; this.age = age; } function Student(name, age) { People.call(this,name,age); } var student = new Student("xiaoming", 20); console.log(student); ``` ## 構造函數繼承的優缺點 優點: 1. 解決子類共享父類引用屬性的問題 2. 解決創建子類實例時,不可以向父類傳遞參數 3. 可以實現多繼承(call多個父類對象) 缺點: 1. 只能繼承父類的實例屬性和方法,不能繼承原型屬性/方法 2. 無法實現函數復用,每個子類都有父類實例函數的副本,影響性能 ## 組合構造函數+原型 ``` function People(name, age) { this.name = name; this.age = age; // 僅在第一次調用的初始化 if (typeof this.say !== 'function') { People.prototype.say = function() { console.log("haha"); } } } function Student(name, age) { People.call(this,name,age); } Student.prototype = new People(); var student = new Student("xiaoming", 20); console.log(student); ``` ## 優缺點 優點: 1. 解決子類共享父類引用屬性的問題 2. 可以向父類傳遞參數 3. 函數可以復用 缺點: 1. 調用了兩次父類構造函數,生成了兩份實例(僅僅多消耗一點內存) ## 寄生組合模式 ``` function obj(o) { function F() {}; F.prototype = o; return new F(); } function People(name,age) { this.name = name; this.age = age; // 僅在第一次調用的初始化 if (typeof this.say !== 'function') { People.prototype.say = function() { console.log("haha"); } } } function Student(name, age) { People.call(this,name,age); } var f= obj(People.prototype); f.constructor = Student; Student.prototype = f; var student = new Student("xiaoming", 20); student.say(); ``` ## 優缺點 優點: 最佳 缺點: 實現較為復雜
                  <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>

                              哎呀哎呀视频在线观看