<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之旅 廣告
                # Class 1. [Class基本語法](http://es6.ruanyifeng.com/#docs/class#Class基本語法) 2. [Class的繼承](http://es6.ruanyifeng.com/#docs/class#Class的繼承) 3. [原生構造函數的繼承](http://es6.ruanyifeng.com/#docs/class#原生構造函數的繼承) 4. [Class的取值函數(getter)和存值函數(setter)](http://es6.ruanyifeng.com/#docs/class#Class的取值函數(getter)和存值函數(setter)) 5. [Class的Generator方法](http://es6.ruanyifeng.com/#docs/class#Class的Generator方法) 6. [Class的靜態方法](http://es6.ruanyifeng.com/#docs/class#Class的靜態方法) 7. [Class的靜態屬性和實例屬性](http://es6.ruanyifeng.com/#docs/class#Class的靜態屬性和實例屬性) 8. [new.target屬性](http://es6.ruanyifeng.com/#docs/class#new.target屬性) 9. [Mixin模式的實現](http://es6.ruanyifeng.com/#docs/class#Mixin模式的實現) ## Class基本語法 ### 概述 JavaScript語言的傳統方法是通過構造函數,定義并生成新對象。下面是一個例子。 ~~~ function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function () { return '(' + this.x + ', ' + this.y + ')'; }; var p = new Point(1, 2); ~~~ 上面這種寫法跟傳統的面向對象語言(比如C++和Java)差異很大,很容易讓新學習這門語言的程序員感到困惑。 ES6提供了更接近傳統語言的寫法,引入了Class(類)這個概念,作為對象的模板。通過`class`關鍵字,可以定義類。基本上,ES6的`class`可以看作只是一個語法糖,它的絕大部分功能,ES5都可以做到,新的`class`寫法只是讓對象原型的寫法更加清晰、更像面向對象編程的語法而已。上面的代碼用ES6的“類”改寫,就是下面這樣。 ~~~ //定義類 class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } ~~~ 上面代碼定義了一個“類”,可以看到里面有一個`constructor`方法,這就是構造方法,而`this`關鍵字則代表實例對象。也就是說,ES5的構造函數`Point`,對應ES6的`Point`類的構造方法。 `Point`類除了構造方法,還定義了一個`toString`方法。注意,定義“類”的方法的時候,前面不需要加上`function`這個關鍵字,直接把函數定義放進去了就可以了。另外,方法之間不需要逗號分隔,加了會報錯。 ES6的類,完全可以看作構造函數的另一種寫法。 ~~~ class Point { // ... } typeof Point // "function" Point === Point.prototype.constructor // true ~~~ 上面代碼表明,類的數據類型就是函數,類本身就指向構造函數。 使用的時候,也是直接對類使用`new`命令,跟構造函數的用法完全一致。 ~~~ class Bar { doStuff() { console.log('stuff'); } } var b = new Bar(); b.doStuff() // "stuff" ~~~ 構造函數的`prototype`屬性,在ES6的“類”上面繼續存在。事實上,類的所有方法都定義在類的`prototype`屬性上面。 ~~~ class Point { constructor(){ // ... } toString(){ // ... } toValue(){ // ... } } // 等同于 Point.prototype = { toString(){}, toValue(){} }; ~~~ 在類的實例上面調用方法,其實就是調用原型上的方法。 ~~~ class B {} let b = new B(); b.constructor === B.prototype.constructor // true ~~~ 上面代碼中,`b`是B類的實例,它的`constructor`方法就是B類原型的`constructor`方法。 由于類的方法都定義在`prototype`對象上面,所以類的新方法可以添加在`prototype`對象上面。`Object.assign`方法可以很方便地一次向類添加多個方法。 ~~~ class Point { constructor(){ // ... } } Object.assign(Point.prototype, { toString(){}, toValue(){} }); ~~~ `prototype`對象的`constructor`屬性,直接指向“類”的本身,這與ES5的行為是一致的。 ~~~ Point.prototype.constructor === Point // true ~~~ 另外,類的內部所有定義的方法,都是不可枚舉的(non-enumerable)。 ~~~ class Point { constructor(x, y) { // ... } toString() { // ... } } Object.keys(Point.prototype) // [] Object.getOwnPropertyNames(Point.prototype) // ["constructor","toString"] ~~~ 上面代碼中,`toString`方法是`Point`類內部定義的方法,它是不可枚舉的。這一點與ES5的行為不一致。 ~~~ var Point = function (x, y) { // ... }; Point.prototype.toString = function() { // ... }; Object.keys(Point.prototype) // ["toString"] Object.getOwnPropertyNames(Point.prototype) // ["constructor","toString"] ~~~ 上面代碼采用ES5的寫法,`toString`方法就是可枚舉的。 類的屬性名,可以采用表達式。 ~~~ let methodName = "getArea"; class Square{ constructor(length) { // ... } [methodName]() { // ... } } ~~~ 上面代碼中,`Square`類的方法名`getArea`,是從表達式得到的。 ### constructor方法 `constructor`方法是類的默認方法,通過`new`命令生成對象實例時,自動調用該方法。一個類必須有`constructor`方法,如果沒有顯式定義,一個空的`constructor`方法會被默認添加。 ~~~ constructor() {} ~~~ `constructor`方法默認返回實例對象(即`this`),完全可以指定返回另外一個對象。 ~~~ class Foo { constructor() { return Object.create(null); } } new Foo() instanceof Foo // false ~~~ 上面代碼中,`constructor`函數返回一個全新的對象,結果導致實例對象不是`Foo`類的實例。 類的構造函數,不使用`new`是沒法調用的,會報錯。這是它跟普通構造函數的一個主要區別,后者不用`new`也可以執行。 ~~~ class Foo { constructor() { return Object.create(null); } } Foo() // TypeError: Class constructor Foo cannot be invoked without 'new' ~~~ ### 類的實例對象 生成類的實例對象的寫法,與ES5完全一樣,也是使用`new`命令。如果忘記加上`new`,像函數那樣調用`Class`,將會報錯。 ~~~ // 報錯 var point = Point(2, 3); // 正確 var point = new Point(2, 3); ~~~ 與ES5一樣,實例的屬性除非顯式定義在其本身(即定義在`this`對象上),否則都是定義在原型上(即定義在`class`上)。 ~~~ //定義類 class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } var point = new Point(2, 3); point.toString() // (2, 3) point.hasOwnProperty('x') // true point.hasOwnProperty('y') // true point.hasOwnProperty('toString') // false point.__proto__.hasOwnProperty('toString') // true ~~~ 上面代碼中,`x`和`y`都是實例對象`point`自身的屬性(因為定義在`this`變量上),所以`hasOwnProperty`方法返回`true`,而`toString`是原型對象的屬性(因為定義在`Point`類上),所以`hasOwnProperty`方法返回`false`。這些都與ES5的行為保持一致。 與ES5一樣,類的所有實例共享一個原型對象。 ~~~ var p1 = new Point(2,3); var p2 = new Point(3,2); p1.__proto__ === p2.__proto__ //true ~~~ 上面代碼中,`p1`和`p2`都是Point的實例,它們的原型都是Point,所以`__proto__`屬性是相等的。 這也意味著,可以通過實例的`__proto__`屬性為Class添加方法。 ~~~ var p1 = new Point(2,3); var p2 = new Point(3,2); p1.__proto__.printName = function () { return 'Oops' }; p1.printName() // "Oops" p2.printName() // "Oops" var p3 = new Point(4,2); p3.printName() // "Oops" ~~~ 上面代碼在`p1`的原型上添加了一個`printName`方法,由于`p1`的原型就是`p2`的原型,因此`p2`也可以調用這個方法。而且,此后新建的實例`p3`也可以調用這個方法。這意味著,使用實例的`__proto__`屬性改寫原型,必須相當謹慎,不推薦使用,因為這會改變Class的原始定義,影響到所有實例。 ### 不存在變量提升 Class不存在變量提升(hoist),這一點與ES5完全不同。 ~~~ new Foo(); // ReferenceError class Foo {} ~~~ 上面代碼中,`Foo`類使用在前,定義在后,這樣會報錯,因為ES6不會把類的聲明提升到代碼頭部。這種規定的原因與下文要提到的繼承有關,必須保證子類在父類之后定義。 ~~~ { let Foo = class {}; class Bar extends Foo { } } ~~~ 上面的代碼不會報錯,因為`class`繼承`Foo`的時候,`Foo`已經有定義了。但是,如果存在`class`的提升,上面代碼就會報錯,因為`class`會被提升到代碼頭部,而`let`命令是不提升的,所以導致`class`繼承`Foo`的時候,`Foo`還沒有定義。 ### Class表達式 與函數一樣,類也可以使用表達式的形式定義。 ~~~ const MyClass = class Me { getClassName() { return Me.name; } }; ~~~ 上面代碼使用表達式定義了一個類。需要注意的是,這個類的名字是`MyClass`而不是`Me`,`Me`只在Class的內部代碼可用,指代當前類。 ~~~ let inst = new MyClass(); inst.getClassName() // Me Me.name // ReferenceError: Me is not defined ~~~ 上面代碼表示,`Me`只在Class內部有定義。 如果類的內部沒用到的話,可以省略`Me`,也就是可以寫成下面的形式。 ~~~ const MyClass = class { /* ... */ }; ~~~ 采用Class表達式,可以寫出立即執行的Class。 ~~~ let person = new class { constructor(name) { this.name = name; } sayName() { console.log(this.name); } }('張三'); person.sayName(); // "張三" ~~~ 上面代碼中,`person`是一個立即執行的類的實例。 ### 私有方法 私有方法是常見需求,但ES6不提供,只能通過變通方法模擬實現。 一種做法是在命名上加以區別。 ~~~ class Widget { // 公有方法 foo (baz) { this._bar(baz); } // 私有方法 _bar(baz) { return this.snaf = baz; } // ... } ~~~ 上面代碼中,`_bar`方法前面的下劃線,表示這是一個只限于內部使用的私有方法。但是,這種命名是不保險的,在類的外部,還是可以調用到這個方法。 另一種方法就是索性將私有方法移出模塊,因為模塊內部的所有方法都是對外可見的。 ~~~ class Widget { foo (baz) { bar.call(this, baz); } // ... } function bar(baz) { return this.snaf = baz; } ~~~ 上面代碼中,`foo`是公有方法,內部調用了`bar.call(this, baz)`。這使得`bar`實際上成為了當前模塊的私有方法。 還有一種方法是利用`Symbol`值的唯一性,將私有方法的名字命名為一個`Symbol`值。 ~~~ const bar = Symbol('bar'); const snaf = Symbol('snaf'); export default class myClass{ // 公有方法 foo(baz) { this[bar](baz); } // 私有方法 [bar](baz) { return this[snaf] = baz; } // ... }; ~~~ 上面代碼中,`bar`和`snaf`都是`Symbol`值,導致第三方無法獲取到它們,因此達到了私有方法和私有屬性的效果。 ### this的指向 類的方法內部如果含有`this`,它默認指向類的實例。但是,必須非常小心,一旦單獨使用該方法,很可能報錯。 ~~~ class Logger { printName(name = 'there') { this.print(`Hello ${name}`); } print(text) { console.log(text); } } const logger = new Logger(); const { printName } = logger; printName(); // TypeError: Cannot read property 'print' of undefined ~~~ 上面代碼中,`printName`方法中的`this`,默認指向`Logger`類的實例。但是,如果將這個方法提取出來單獨使用,`this`會指向該方法運行時所在的環境,因為找不到`print`方法而導致報錯。 一個比較簡單的解決方法是,在構造方法中綁定`this`,這樣就不會找不到`print`方法了。 ~~~ class Logger { constructor() { this.printName = this.printName.bind(this); } // ... } ~~~ 另一種解決方法是使用箭頭函數。 ~~~ class Logger { constructor() { this.printName = (name = 'there') => { this.print(`Hello ${name}`); }; } // ... } ~~~ 還有一種解決方法是使用`Proxy`,獲取方法的時候,自動綁定`this`。 ~~~ function selfish (target) { const cache = new WeakMap(); const handler = { get (target, key) { const value = Reflect.get(target, key); if (typeof value !== 'function') { return value; } if (!cache.has(value)) { cache.set(value, value.bind(target)); } return cache.get(value); } }; const proxy = new Proxy(target, handler); return proxy; } const logger = selfish(new Logger()); ~~~ ### 嚴格模式 類和模塊的內部,默認就是嚴格模式,所以不需要使用`use strict`指定運行模式。只要你的代碼寫在類或模塊之中,就只有嚴格模式可用。 考慮到未來所有的代碼,其實都是運行在模塊之中,所以ES6實際上把整個語言升級到了嚴格模式。 ### name屬性 由于本質上,ES6的類只是ES5的構造函數的一層包裝,所以函數的許多特性都被`Class`繼承,包括`name`屬性。 ~~~ class Point {} Point.name // "Point" ~~~ `name`屬性總是返回緊跟在`class`關鍵字后面的類名。 ## Class的繼承 ### 基本用法 Class之間可以通過`extends`關鍵字實現繼承,這比ES5的通過修改原型鏈實現繼承,要清晰和方便很多。 ~~~ class ColorPoint extends Point {} ~~~ 上面代碼定義了一個`ColorPoint`類,該類通過`extends`關鍵字,繼承了`Point`類的所有屬性和方法。但是由于沒有部署任何代碼,所以這兩個類完全一樣,等于復制了一個`Point`類。下面,我們在`ColorPoint`內部加上代碼。 ~~~ class ColorPoint extends Point { constructor(x, y, color) { super(x, y); // 調用父類的constructor(x, y) this.color = color; } toString() { return this.color + ' ' + super.toString(); // 調用父類的toString() } } ~~~ 上面代碼中,`constructor`方法和`toString`方法之中,都出現了`super`關鍵字,它在這里表示父類的構造函數,用來新建父類的`this`對象。 子類必須在`constructor`方法中調用`super`方法,否則新建實例時會報錯。這是因為子類沒有自己的`this`對象,而是繼承父類的`this`對象,然后對其進行加工。如果不調用`super`方法,子類就得不到`this`對象。 ~~~ class Point { /* ... */ } class ColorPoint extends Point { constructor() { } } let cp = new ColorPoint(); // ReferenceError ~~~ 上面代碼中,`ColorPoint`繼承了父類`Point`,但是它的構造函數沒有調用`super`方法,導致新建實例時報錯。 ES5的繼承,實質是先創造子類的實例對象`this`,然后再將父類的方法添加到`this`上面(`Parent.apply(this)`)。ES6的繼承機制完全不同,實質是先創造父類的實例對象`this`(所以必須先調用`super`方法),然后再用子類的構造函數修改`this`。 如果子類沒有定義`constructor`方法,這個方法會被默認添加,代碼如下。也就是說,不管有沒有顯式定義,任何一個子類都有`constructor`方法。 ~~~ constructor(...args) { super(...args); } ~~~ 另一個需要注意的地方是,在子類的構造函數中,只有調用`super`之后,才可以使用`this`關鍵字,否則會報錯。這是因為子類實例的構建,是基于對父類實例加工,只有`super`方法才能返回父類實例。 ~~~ class Point { constructor(x, y) { this.x = x; this.y = y; } } class ColorPoint extends Point { constructor(x, y, color) { this.color = color; // ReferenceError super(x, y); this.color = color; // 正確 } } ~~~ 上面代碼中,子類的`constructor`方法沒有調用`super`之前,就使用`this`關鍵字,結果報錯,而放在`super`方法之后就是正確的。 下面是生成子類實例的代碼。 ~~~ let cp = new ColorPoint(25, 8, 'green'); cp instanceof ColorPoint // true cp instanceof Point // true ~~~ 上面代碼中,實例對象`cp`同時是`ColorPoint`和`Point`兩個類的實例,這與ES5的行為完全一致。 ### 類的prototype屬性和__proto__屬性 大多數瀏覽器的ES5實現之中,每一個對象都有`__proto__`屬性,指向對應的構造函數的prototype屬性。Class作為構造函數的語法糖,同時有prototype屬性和`__proto__`屬性,因此同時存在兩條繼承鏈。 (1)子類的`__proto__`屬性,表示構造函數的繼承,總是指向父類。 (2)子類`prototype`屬性的`__proto__`屬性,表示方法的繼承,總是指向父類的`prototype`屬性。 ~~~ class A { } class B extends A { } B.__proto__ === A // true B.prototype.__proto__ === A.prototype // true ~~~ 上面代碼中,子類`B`的`__proto__`屬性指向父類`A`,子類`B`的`prototype`屬性的`__proto__`屬性指向父類`A`的`prototype`屬性。 這樣的結果是因為,類的繼承是按照下面的模式實現的。 ~~~ class A { } class B { } // B的實例繼承A的實例 Object.setPrototypeOf(B.prototype, A.prototype); // B繼承A的靜態屬性 Object.setPrototypeOf(B, A); ~~~ 《對象的擴展》一章給出過`Object.setPrototypeOf`方法的實現。 ~~~ Object.setPrototypeOf = function (obj, proto) { obj.__proto__ = proto; return obj; } ~~~ 因此,就得到了上面的結果。 ~~~ Object.setPrototypeOf(B.prototype, A.prototype); // 等同于 B.prototype.__proto__ = A.prototype; Object.setPrototypeOf(B, A); // 等同于 B.__proto__ = A; ~~~ 這兩條繼承鏈,可以這樣理解:作為一個對象,子類(`B`)的原型(`__proto__`屬性)是父類(`A`);作為一個構造函數,子類(`B`)的原型(`prototype`屬性)是父類的實例。 ~~~ Object.create(A.prototype); // 等同于 B.prototype.__proto__ = A.prototype; ~~~ ### Extends 的繼承目標 `extends`關鍵字后面可以跟多種類型的值。 ~~~ class B extends A { } ~~~ 上面代碼的`A`,只要是一個有`prototype`屬性的函數,就能被`B`繼承。由于函數都有`prototype`屬性(除了`Function.prototype`函數),因此`A`可以是任意函數。 下面,討論三種特殊情況。 第一種特殊情況,子類繼承Object類。 ~~~ class A extends Object { } A.__proto__ === Object // true A.prototype.__proto__ === Object.prototype // true ~~~ 這種情況下,`A`其實就是構造函數`Object`的復制,`A`的實例就是`Object`的實例。 第二種特殊情況,不存在任何繼承。 ~~~ class A { } A.__proto__ === Function.prototype // true A.prototype.__proto__ === Object.prototype // true ~~~ 這種情況下,A作為一個基類(即不存在任何繼承),就是一個普通函數,所以直接繼承`Funciton.prototype`。但是,`A`調用后返回一個空對象(即`Object`實例),所以`A.prototype.__proto__`指向構造函數(`Object`)的`prototype`屬性。 第三種特殊情況,子類繼承`null`。 ~~~ class A extends null { } A.__proto__ === Function.prototype // true A.prototype.__proto__ === undefined // true ~~~ 這種情況與第二種情況非常像。`A`也是一個普通函數,所以直接繼承`Funciton.prototype`。但是,A調用后返回的對象不繼承任何方法,所以它的`__proto__`指向`Function.prototype`,即實質上執行了下面的代碼。 ~~~ class C extends null { constructor() { return Object.create(null); } } ~~~ ### Object.getPrototypeOf() `Object.getPrototypeOf`方法可以用來從子類上獲取父類。 ~~~ Object.getPrototypeOf(ColorPoint) === Point // true ~~~ 因此,可以使用這個方法判斷,一個類是否繼承了另一個類。 ### super關鍵字 `super`這個關鍵字,有兩種用法,含義不同。 (1)作為函數調用時(即`super(...args)`),`super`代表父類的構造函數。 (2)作為對象調用時(即`super.prop`或`super.method()`),`super`代表父類。注意,此時`super`即可以引用父類實例的屬性和方法,也可以引用父類的靜態方法。 ~~~ class B extends A { get m() { return this._p * super._p; } set m() { throw new Error('該屬性只讀'); } } ~~~ 上面代碼中,子類通過`super`關鍵字,調用父類實例的`_p`屬性。 由于,對象總是繼承其他對象的,所以可以在任意一個對象中,使用`super`關鍵字。 ~~~ var obj = { toString() { return "MyObject: " + super.toString(); } }; obj.toString(); // MyObject: [object Object] ~~~ ### 實例的__proto__屬性 子類實例的__proto__屬性的__proto__屬性,指向父類實例的__proto__屬性。也就是說,子類的原型的原型,是父類的原型。 ~~~ var p1 = new Point(2, 3); var p2 = new ColorPoint(2, 3, 'red'); p2.__proto__ === p1.__proto__ // false p2.__proto__.__proto__ === p1.__proto__ // true ~~~ 上面代碼中,`ColorPoint`繼承了`Point`,導致前者原型的原型是后者的原型。 因此,通過子類實例的`__proto__.__proto__`屬性,可以修改父類實例的行為。 ~~~ p2.__proto__.__proto__.printName = function () { console.log('Ha'); }; p1.printName() // "Ha" ~~~ 上面代碼在`ColorPoint`的實例`p2`上向`Point`類添加方法,結果影響到了`Point`的實例`p1`。 ## 原生構造函數的繼承 原生構造函數是指語言內置的構造函數,通常用來生成數據結構。ECMAScript的原生構造函數大致有下面這些。 * Boolean() * Number() * String() * Array() * Date() * Function() * RegExp() * Error() * Object() 以前,這些原生構造函數是無法繼承的,比如,不能自己定義一個`Array`的子類。 ~~~ function MyArray() { Array.apply(this, arguments); } MyArray.prototype = Object.create(Array.prototype, { constructor: { value: MyArray, writable: true, configurable: true, enumerable: true } }); ~~~ 上面代碼定義了一個繼承Array的`MyArray`類。但是,這個類的行為與`Array`完全不一致。 ~~~ var colors = new MyArray(); colors[0] = "red"; colors.length // 0 colors.length = 0; colors[0] // "red" ~~~ 之所以會發生這種情況,是因為子類無法獲得原生構造函數的內部屬性,通過`Array.apply()`或者分配給原型對象都不行。原生構造函數會忽略`apply`方法傳入的`this`,也就是說,原生構造函數的`this`無法綁定,導致拿不到內部屬性。 ES5是先新建子類的實例對象`this`,再將父類的屬性添加到子類上,由于父類的內部屬性無法獲取,導致無法繼承原生的構造函數。比如,Array構造函數有一個內部屬性`[[DefineOwnProperty]]`,用來定義新屬性時,更新`length`屬性,這個內部屬性無法在子類獲取,導致子類的`length`屬性行為不正常。 下面的例子中,我們想讓一個普通對象繼承`Error`對象。 ~~~ var e = {}; Object.getOwnPropertyNames(Error.call(e)) // [ 'stack' ] Object.getOwnPropertyNames(e) // [] ~~~ 上面代碼中,我們想通過`Error.call(e)`這種寫法,讓普通對象`e`具有`Error`對象的實例屬性。但是,`Error.call()`完全忽略傳入的第一個參數,而是返回一個新對象,`e`本身沒有任何變化。這證明了`Error.call(e)`這種寫法,無法繼承原生構造函數。 ES6允許繼承原生構造函數定義子類,因為ES6是先新建父類的實例對象`this`,然后再用子類的構造函數修飾`this`,使得父類的所有行為都可以繼承。下面是一個繼承`Array`的例子。 ~~~ class MyArray extends Array { constructor(...args) { super(...args); } } var arr = new MyArray(); arr[0] = 12; arr.length // 1 arr.length = 0; arr[0] // undefined ~~~ 上面代碼定義了一個`MyArray`類,繼承了`Array`構造函數,因此就可以從`MyArray`生成數組的實例。這意味著,ES6可以自定義原生數據結構(比如Array、String等)的子類,這是ES5無法做到的。 上面這個例子也說明,`extends`關鍵字不僅可以用來繼承類,還可以用來繼承原生的構造函數。因此可以在原生數據結構的基礎上,定義自己的數據結構。下面就是定義了一個帶版本功能的數組。 ~~~ class VersionedArray extends Array { constructor() { super(); this.history = [[]]; } commit() { this.history.push(this.slice()); } revert() { this.splice(0, this.length, ...this.history[this.history.length - 1]); } } var x = new VersionedArray(); x.push(1); x.push(2); x // [1, 2] x.history // [[]] x.commit(); x.history // [[], [1, 2]] x.push(3); x // [1, 2, 3] x.revert(); x // [1, 2] ~~~ 上面代碼中,`VersionedArray`結構會通過`commit`方法,將自己的當前狀態存入`history`屬性,然后通過`revert`方法,可以撤銷當前版本,回到上一個版本。除此之外,`VersionedArray`依然是一個數組,所有原生的數組方法都可以在它上面調用。 下面是一個自定義`Error`子類的例子。 ~~~ class ExtendableError extends Error { constructor(message) { super(); this.message = message; this.stack = (new Error()).stack; this.name = this.constructor.name; } } class MyError extends ExtendableError { constructor(m) { super(m); } } var myerror = new MyError('ll'); myerror.message // "ll" myerror instanceof Error // true myerror.name // "MyError" myerror.stack // Error // at MyError.ExtendableError // ... ~~~ 注意,繼承`Object`的子類,有一個[行為差異](http://stackoverflow.com/questions/36203614/super-does-not-pass-arguments-when-instantiating-a-class-extended-from-object)。 ~~~ class NewObj extends Object{ constructor(){ super(...arguments); } } var o = new NewObj({attr: true}); console.log(o.attr === true); // false ~~~ 上面代碼中,`NewObj`繼承了`Object`,但是無法通過`super`方法向父類`Object`傳參。這是因為ES6改變了`Object`構造函數的行為,一旦發現`Object`方法不是通過`new Object()`這種形式調用,ES6規定`Object`構造函數會忽略參數。 ## Class的取值函數(getter)和存值函數(setter) 與ES5一樣,在Class內部可以使用`get`和`set`關鍵字,對某個屬性設置存值函數和取值函數,攔截該屬性的存取行為。 ~~~ class MyClass { constructor() { // ... } get prop() { return 'getter'; } set prop(value) { console.log('setter: '+value); } } let inst = new MyClass(); inst.prop = 123; // setter: 123 inst.prop // 'getter' ~~~ 上面代碼中,`prop`屬性有對應的存值函數和取值函數,因此賦值和讀取行為都被自定義了。 存值函數和取值函數是設置在屬性的descriptor對象上的。 ~~~ class CustomHTMLElement { constructor(element) { this.element = element; } get html() { return this.element.innerHTML; } set html(value) { this.element.innerHTML = value; } } var descriptor = Object.getOwnPropertyDescriptor( CustomHTMLElement.prototype, "html"); "get" in descriptor // true "set" in descriptor // true ~~~ 上面代碼中,存值函數和取值函數是定義在`html`屬性的描述對象上面,這與ES5完全一致。 ## Class的Generator方法 如果某個方法之前加上星號(`*`),就表示該方法是一個Generator函數。 ~~~ class Foo { constructor(...args) { this.args = args; } * [Symbol.iterator]() { for (let arg of this.args) { yield arg; } } } for (let x of new Foo('hello', 'world')) { console.log(x); } // hello // world ~~~ 上面代碼中,Foo類的Symbol.iterator方法前有一個星號,表示該方法是一個Generator函數。Symbol.iterator方法返回一個Foo類的默認遍歷器,for...of循環會自動調用這個遍歷器。 ## Class的靜態方法 類相當于實例的原型,所有在類中定義的方法,都會被實例繼承。如果在一個方法前,加上`static`關鍵字,就表示該方法不會被實例繼承,而是直接通過類來調用,這就稱為“靜態方法”。 ~~~ class Foo { static classMethod() { return 'hello'; } } Foo.classMethod() // 'hello' var foo = new Foo(); foo.classMethod() // TypeError: foo.classMethod is not a function ~~~ 上面代碼中,`Foo`類的`classMethod`方法前有`static`關鍵字,表明該方法是一個靜態方法,可以直接在`Foo`類上調用(`Foo.classMethod()`),而不是在`Foo`類的實例上調用。如果在實例上調用靜態方法,會拋出一個錯誤,表示不存在該方法。 父類的靜態方法,可以被子類繼承。 ~~~ class Foo { static classMethod() { return 'hello'; } } class Bar extends Foo { } Bar.classMethod(); // 'hello' ~~~ 上面代碼中,父類`Foo`有一個靜態方法,子類`Bar`可以調用這個方法。 靜態方法也是可以從`super`對象上調用的。 ~~~ class Foo { static classMethod() { return 'hello'; } } class Bar extends Foo { static classMethod() { return super.classMethod() + ', too'; } } Bar.classMethod(); ~~~ ## Class的靜態屬性和實例屬性 靜態屬性指的是Class本身的屬性,即`Class.propname`,而不是定義在實例對象(`this`)上的屬性。 ~~~ class Foo { } Foo.prop = 1; Foo.prop // 1 ~~~ 上面的寫法為`Foo`類定義了一個靜態屬性`prop`。 目前,只有這種寫法可行,因為ES6明確規定,Class內部只有靜態方法,沒有靜態屬性。 ~~~ // 以下兩種寫法都無效 class Foo { // 寫法一 prop: 2 // 寫法二 static prop: 2 } Foo.prop // undefined ~~~ ES7有一個靜態屬性的[提案](https://github.com/jeffmo/es-class-properties),目前Babel轉碼器支持。 這個提案對實例屬性和靜態屬性,都規定了新的寫法。 (1)類的實例屬性 類的實例屬性可以用等式,寫入類的定義之中。 ~~~ class MyClass { myProp = 42; constructor() { console.log(this.myProp); // 42 } } ~~~ 上面代碼中,`myProp`就是`MyClass`的實例屬性。在`MyClass`的實例上,可以讀取這個屬性。 以前,我們定義實例屬性,只能寫在類的`constructor`方法里面。 ~~~ class ReactCounter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } } ~~~ 上面代碼中,構造方法`constructor`里面,定義了`this.state`屬性。 有了新的寫法以后,可以不在`constructor`方法里面定義。 ~~~ class ReactCounter extends React.Component { state = { count: 0 }; } ~~~ 這種寫法比以前更清晰。 為了可讀性的目的,對于那些在`constructor`里面已經定義的實例屬性,新寫法允許直接列出。 ~~~ class ReactCounter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } state; } ~~~ (2)類的靜態屬性 類的靜態屬性只要在上面的實例屬性寫法前面,加上`static`關鍵字就可以了。 ~~~ class MyClass { static myStaticProp = 42; constructor() { console.log(MyClass.myProp); // 42 } } ~~~ 同樣的,這個新寫法大大方便了靜態屬性的表達。 ~~~ // 老寫法 class Foo { } Foo.prop = 1; // 新寫法 class Foo { static prop = 1; } ~~~ 上面代碼中,老寫法的靜態屬性定義在類的外部。整個類生成以后,再生成靜態屬性。這樣讓人很容易忽略這個靜態屬性,也不符合相關代碼應該放在一起的代碼組織原則。另外,新寫法是顯式聲明(declarative),而不是賦值處理,語義更好。 ## new.target屬性 `new`是從構造函數生成實例的命令。ES6為`new`命令引入了一個`new.target`屬性,(在構造函數中)返回`new`命令作用于的那個構造函數。如果構造函數不是通過`new`命令調用的,`new.target`會返回`undefined`,因此這個屬性可以用來確定構造函數是怎么調用的。 ~~~ function Person(name) { if (new.target !== undefined) { this.name = name; } else { throw new Error('必須使用new生成實例'); } } // 另一種寫法 function Person(name) { if (new.target === Person) { this.name = name; } else { throw new Error('必須使用new生成實例'); } } var person = new Person('張三'); // 正確 var notAPerson = Person.call(person, '張三'); // 報錯 ~~~ 上面代碼確保構造函數只能通過`new`命令調用。 Class內部調用`new.target`,返回當前Class。 ~~~ class Rectangle { constructor(length, width) { console.log(new.target === Rectangle); this.length = length; this.width = width; } } var obj = new Rectangle(3, 4); // 輸出 true ~~~ 需要注意的是,子類繼承父類時,`new.target`會返回子類。 ~~~ class Rectangle { constructor(length, width) { console.log(new.target === Rectangle); // ... } } class Square extends Rectangle { constructor(length) { super(length, length); } } var obj = new Square(3); // 輸出 false ~~~ 上面代碼中,`new.target`會返回子類。 利用這個特點,可以寫出不能獨立使用、必須繼承后才能使用的類。 ~~~ class Shape { constructor() { if (new.target === Shape) { throw new Error('本類不能實例化'); } } } class Rectangle extends Shape { constructor(length, width) { super(); // ... } } var x = new Shape(); // 報錯 var y = new Rectangle(3, 4); // 正確 ~~~ 上面代碼中,`Shape`類不能被實例化,只能用于繼承。 注意,在函數外部,使用`new.target`會報錯。 ## Mixin模式的實現 Mixin模式指的是,將多個類的接口“混入”(mix in)另一個類。它在ES6的實現如下。 ~~~ function mix(...mixins) { class Mix {} for (let mixin of mixins) { copyProperties(Mix, mixin); copyProperties(Mix.prototype, mixin.prototype); } return Mix; } function copyProperties(target, source) { for (let key of Reflect.ownKeys(source)) { if ( key !== "constructor" && key !== "prototype" && key !== "name" ) { let desc = Object.getOwnPropertyDescriptor(source, key); Object.defineProperty(target, key, desc); } } } ~~~ 上面代碼的`mix`函數,可以將多個對象合成為一個類。使用的時候,只要繼承這個類即可。 ~~~ class DistributedEdit extends mix(Loggable, Serializable) { // ... } ~~~
                  <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>

                              哎呀哎呀视频在线观看