<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 語法 ~~~ constructor([arguments]) { ... } ~~~ ## 描述 在一個類中只能有一個名為 “constructor” 的特殊方法。 一個類中出現多次構造函數 (`constructor)`方法將會拋出一個[`SyntaxError`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError "SyntaxError?對象代表嘗試解析語法上不合法的代碼的錯誤。")錯誤。 在一個構造方法中可以使用`super`關鍵字來調用一個父類的構造方法。 如果沒有顯式指定構造方法,則會添加默認的 constructor 方法。 如果不指定一個構造函數(constructor)方法, 則使用一個默認的構造函數(constructor)。 ## 示例 ### 使用`constructor`方法 ~~~js class Square extends Polygon { constructor(length) { // 在這里, 它調用了父類的構造函數, 并將 lengths 提供給 Polygon 的"width"和"height" super(length, length); // 注意: 在派生類中, 必須先調用 super() 才能使用 "this"。 // 忽略這個,將會導致一個引用錯誤。 this.name = 'Square'; } get area() { return this.height * this.width; } set area(value) { // 注意:不可使用 this.area = value // 否則會導致循環call setter方法導致爆棧 this._area = value; } } ~~~ ### 另一個例子 看看這個代碼片段 ~~~js class Polygon { constructor() { this.name = "Polygon"; } } class Square extends Polygon { constructor() { super(); } } class Rectangle {} Object.setPrototypeOf(Square.prototype, Rectangle.prototype); console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true let newInstance = new Square(); console.log(newInstance.name); //Polygon ~~~ 這里,**Square**類的原型被改變,但是在正在創建一個新的正方形實例時,仍然調用前一個基類**Polygon**的構造函數。 ### 默認構造方法 如前所述,如果不指定構造方法,則使用默認構造函數。對于基類,默認構造函數是: ~~~js constructor() {} ~~~ 對于派生類,默認構造函數是: ~~~js constructor(...args) { super(...args); } ~~~
                  <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>

                              哎呀哎呀视频在线观看