新聞中心
1. 簡(jiǎn)介
Class 可以通過(guò)extends關(guān)鍵字實(shí)現(xiàn)繼承,這比 ES5 的通過(guò)修改原型鏈實(shí)現(xiàn)繼承,要清晰和方便很多。

創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供貢山企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站建設(shè)、做網(wǎng)站、H5建站、小程序制作等業(yè)務(wù)。10年已為貢山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
class Point {
}
class ColorPoint extends Point {
}上面代碼定義了一個(gè) ColorPoint 類(lèi),該類(lèi)通過(guò) extends 關(guān)鍵字,繼承了 Point 類(lèi)的所有屬性和方法。但是由于沒(méi)有部署任何代碼,所以這兩個(gè)類(lèi)完全一樣,等于復(fù)制了一個(gè) Point 類(lèi)。下面,我們?cè)?ColorPoint 內(nèi)部加上代碼。
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y); // 調(diào)用父類(lèi)的constructor(x, y)
this.color = color;
}
toString() {
return this.color + ' ' + super.toString(); // 調(diào)用父類(lèi)的toString()
}
}上面代碼中, constructor 方法和 toString 方法之中,都出現(xiàn)了 super 關(guān)鍵字,它在這里表示父類(lèi)的構(gòu)造函數(shù),用來(lái)新建父類(lèi)的 this 對(duì)象。
子類(lèi)必須在 constructor 方法中調(diào)用 super 方法,否則新建實(shí)例時(shí)會(huì)報(bào)錯(cuò)。這是因?yàn)樽宇?lèi)自己的 this 對(duì)象,必須先通過(guò)父類(lèi)的構(gòu)造函數(shù)完成塑造,得到與父類(lèi)同樣的實(shí)例屬性和方法,然后再對(duì)其進(jìn)行加工,加上子類(lèi)自己的實(shí)例屬性和方法。如果不調(diào)用 super 方法,子類(lèi)就得不到 this 對(duì)象。
class Point { /* ... */ }
class ColorPoint extends Point {
constructor() {
}
}
let cp = new ColorPoint(); // ReferenceError上面代碼中, ColorPoint 繼承了父類(lèi) Point ,但是它的構(gòu)造函數(shù)沒(méi)有調(diào)用 super 方法,導(dǎo)致新建實(shí)例時(shí)報(bào)錯(cuò)。
ES5 的繼承,實(shí)質(zhì)是先創(chuàng)造子類(lèi)的實(shí)例對(duì)象 this ,然后再將父類(lèi)的方法添加到 this 上面( Parent.apply(this) )。es6 的繼承機(jī)制完全不同,實(shí)質(zhì)是先將父類(lèi)實(shí)例對(duì)象的屬性和方法,加到 this 上面(所以必須先調(diào)用 super 方法),然后再用子類(lèi)的構(gòu)造函數(shù)修改 this 。
如果子類(lèi)沒(méi)有定義 constructor 方法,這個(gè)方法會(huì)被默認(rèn)添加,代碼如下。也就是說(shuō),不管有沒(méi)有顯式定義,任何一個(gè)子類(lèi)都有 constructor 方法。
class ColorPoint extends Point {
}
// 等同于
class ColorPoint extends Point {
constructor(...args) {
super(...args);
}
}另一個(gè)需要注意的地方是,在子類(lèi)的構(gòu)造函數(shù)中,只有調(diào)用 super 之后,才可以使用 this 關(guān)鍵字,否則會(huì)報(bào)錯(cuò)。這是因?yàn)樽宇?lèi)實(shí)例的構(gòu)建,基于父類(lèi)實(shí)例,只有 super 方法才能調(diào)用父類(lèi)實(shí)例。
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; // 正確
}
}上面代碼中,子類(lèi)的 constructor 方法沒(méi)有調(diào)用 super 之前,就使用 this 關(guān)鍵字,結(jié)果報(bào)錯(cuò),而放在 super 方法之后就是正確的。
下面是生成子類(lèi)實(shí)例的代碼。
let cp = new ColorPoint(25, 8, 'green');
cp instanceof ColorPoint // true
cp instanceof Point // true上面代碼中,實(shí)例對(duì)象 cp 同時(shí)是 ColorPoint 和 Point 兩個(gè)類(lèi)的實(shí)例,這與 ES5 的行為完全一致。
最后,父類(lèi)的靜態(tài)方法,也會(huì)被子類(lèi)繼承。
class A {
static hello() {
console.log('hello world');
}
}
class B extends A {
}
B.hello() // hello world上面代碼中, hello() 是 A 類(lèi)的靜態(tài)方法, B 繼承 A ,也繼承了 A 的靜態(tài)方法。
2. Object.getPrototypeOf()
Object.getPrototypeOf 方法可以用來(lái)從子類(lèi)上獲取父類(lèi)。
Object.getPrototypeOf(ColorPoint) === Point
// true因此,可以使用這個(gè)方法判斷,一個(gè)類(lèi)是否繼承了另一個(gè)類(lèi)。
3. super 關(guān)鍵字
super這個(gè)關(guān)鍵字,既可以當(dāng)作函數(shù)使用,也可以當(dāng)作對(duì)象使用。在這兩種情況下,它的用法完全不同。
第一種情況, super 作為函數(shù)調(diào)用時(shí),代表父類(lèi)的構(gòu)造函數(shù)。ES6 要求,子類(lèi)的構(gòu)造函數(shù)必須執(zhí)行一次 super 函數(shù)。
class A {}
class B extends A {
constructor() {
super();
}
}上面代碼中,子類(lèi) B 的構(gòu)造函數(shù)之中的 super() ,代表調(diào)用父類(lèi)的構(gòu)造函數(shù)。這是必須的,否則 JavaScript 引擎會(huì)報(bào)錯(cuò)。
注意, super 雖然代表了父類(lèi) A 的構(gòu)造函數(shù),但是返回的是子類(lèi) B 的實(shí)例,即 super 內(nèi)部的 this 指的是 B 的實(shí)例,因此 super() 在這里相當(dāng)于 A.prototype.constructor.call(this) 。
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();
}
}
new A() // A
new B() // B上面代碼中, new.target 指向當(dāng)前正在執(zhí)行的函數(shù)。可以看到,在 super() 執(zhí)行時(shí),它指向的是子類(lèi) B 的構(gòu)造函數(shù),而不是父類(lèi) A 的構(gòu)造函數(shù)。也就是說(shuō), super() 內(nèi)部的 this 指向的是 B 。
作為函數(shù)時(shí), super() 只能用在子類(lèi)的構(gòu)造函數(shù)之中,用在其他地方就會(huì)報(bào)錯(cuò)。
class A {}
class B extends A {
m() {
super(); // 報(bào)錯(cuò)
}
}上面代碼中, super() 用在 B 類(lèi)的 m 方法之中,就會(huì)造成語(yǔ)法錯(cuò)誤。
第二種情況, super 作為對(duì)象時(shí),在普通方法中,指向父類(lèi)的原型對(duì)象;在靜態(tài)方法中,指向父類(lèi)。
class A {
p() {
return 2;
}
}
class B extends A {
constructor() {
super();
console.log(super.p()); // 2
}
}
let b = new B();上面代碼中,子類(lèi) B 當(dāng)中的 super.p() ,就是將 super 當(dāng)作一個(gè)對(duì)象使用。這時(shí), super 在普通方法之中,指向 A.prototype ,所以 super.p() 就相當(dāng)于 A.prototype.p() 。
這里需要注意,由于 super 指向父類(lèi)的原型對(duì)象,所以定義在父類(lèi)實(shí)例上的方法或?qū)傩裕菬o(wú)法通過(guò) super 調(diào)用的。
class A {
constructor() {
this.p = 2;
}
}
class B extends A {
get m() {
return super.p;
}
}
let b = new B();
b.m // undefined上面代碼中, p 是父類(lèi) A 實(shí)例的屬性, super.p 就引用不到它。
如果屬性定義在父類(lèi)的原型對(duì)象上, super 就可以取到。
class A {}
A.prototype.x = 2;
class B extends A {
constructor() {
super();
console.log(super.x) // 2
}
}
let b = new B();上面代碼中,屬性 x 是定義在 A.prototype 上面的,所以 super.x 可以取到它的值。
ES6 規(guī)定,在子類(lèi)普通方法中通過(guò) super 調(diào)用父類(lèi)的方法時(shí),方法內(nèi)部的 this 指向當(dāng)前的子類(lèi)實(shí)例。
class A {
constructor() {
this.x = 1;
}
print() {
console.log(this.x);
}
}
class B extends A {
constructor() {
super();
this.x = 2;
}
m() {
super.print();
}
}
let b = new B();
b.m() // 2上面代碼中, super.print() 雖然調(diào)用的是 A.prototype.print() ,但是 A.prototype.print() 內(nèi)部的 this 指向子類(lèi) B 的實(shí)例,導(dǎo)致輸出的是 2 ,而不是 1 。也就是說(shuō),實(shí)際上執(zhí)行的是 super.print.call(this) 。
由于 this 指向子類(lèi)實(shí)例,所以如果通過(guò) super 對(duì)某個(gè)屬性賦值,這時(shí) super 就是 this ,賦值的屬性會(huì)變成子類(lèi)實(shí)例的屬性。
class A {
constructor() {
this.x = 1;
}
}
class B extends A {
constructor() {
super();
this.x = 2;
super.x = 3;
console.log(super.x); // undefined
console.log(this.x); // 3
}
}
let b = new B();上面代碼中, super.x 賦值為 3 ,這時(shí)等同于對(duì) this.x 賦值為 3 。而當(dāng)讀取 super.x 的時(shí)候,讀的是 A.prototype.x ,所以返回 undefined 。
如果 super 作為對(duì)象,用在靜態(tài)方法之中,這時(shí) super 將指向父類(lèi),而不是父類(lèi)的原型對(duì)象。
class Parent {
static myMethod(msg) {
console.log('static', msg);
}
myMethod(msg) {
console.log('instance', msg);
}
}
class Child extends Parent {
static myMethod(msg) {
super.myMethod(msg);
}
myMethod(msg) {
super.myMethod(msg);
}
}
Child.myMethod(1); // static 1
var child = new Child();
child.myMethod(2); // instance 2上面代碼中, super 在靜態(tài)方法之中指向父類(lèi),在普通方法之中指向父類(lèi)的原型對(duì)象。
另外,在子類(lèi)的靜態(tài)方法中通過(guò) super 調(diào)用父類(lèi)的方法時(shí),方法內(nèi)部的 this 指向當(dāng)前的子類(lèi),而不是子類(lèi)的實(shí)例。
class A {
constructor() {
this.x = 1;
}
static print() {
console.log(this.x);
}
}
class B extends A {
constructor() {
super();
this.x = 2;
}
static m() {
super.print();
}
}
B.x = 3;
B.m() // 3上面代碼中,靜態(tài)方法 B.m 里面, super.print 指向父類(lèi)的靜態(tài)方法。這個(gè)方法里面的 this 指向的是 B ,而不是 B 的實(shí)例。
注意,使用 super 的時(shí)候,必須顯式指定是作為函數(shù)、還是作為對(duì)象使用,否則會(huì)報(bào)錯(cuò)。
class A {}
class B extends A {
constructor() {
super();
console.log(super); // 報(bào)錯(cuò)
}
}上面代碼中, console.log(super) 當(dāng)中的 super ,無(wú)法看出是作為函數(shù)使用,還是作為對(duì)象使用,所以 JavaScript 引擎解析代碼的時(shí)候就會(huì)報(bào)錯(cuò)。這時(shí),如果能清晰地表明 super 的數(shù)據(jù)類(lèi)型,就不會(huì)報(bào)錯(cuò)。
class A {}
class B extends A {
constructor() {
super();
console.log(super.valueOf() instanceof B); // true
}
}
let b = new B();上面代碼中, super.valueOf() 表明 super 是一個(gè)對(duì)象,因此就不會(huì)報(bào)錯(cuò)。同時(shí),由于 super 使得 this 指向 B 的實(shí)例,所以 super.valueOf() 返回的是一個(gè) B 的實(shí)例。
最后,由于對(duì)象總是繼承其他對(duì)象的,所以可以在任意一個(gè)對(duì)象中,使用 super 關(guān)鍵字。
var obj = {
toString() {
return "MyObject: " + super.toString();
}
};
obj.toString(); // MyObject: [object Object]
4. 類(lèi)的 prototype 屬性和proto屬性
大多數(shù)瀏覽器的 ES5 實(shí)現(xiàn)之中,每一個(gè)對(duì)象都有 __proto__ 屬性,指向?qū)?yīng)的構(gòu)造函數(shù)的 prototype 屬性。Class 作為構(gòu)造函數(shù)的語(yǔ)法糖,同時(shí)有 prototype 屬性和 proto 屬性,因此同時(shí)存在兩條繼承鏈。
(1)子類(lèi)的 proto 屬性,表示構(gòu)造函數(shù)的繼承,總是指向父類(lèi)。
(2)子類(lèi) prototype 屬性的 proto 屬性,表示方法的繼承,總是指向父類(lèi)的 prototype 屬性。
class A {
}
class B extends A {
}
B.__proto__ === A // true
B.prototype.__proto__ === A.prototype // true上面代碼中,子類(lèi) B 的 proto 屬性指向父類(lèi) A ,子類(lèi) B 的 prototype 屬性的 proto 屬性指向父類(lèi) A 的 prototype 屬性。
這樣的結(jié)果是因?yàn)椋?lèi)的繼承是按照下面的模式實(shí)現(xiàn)的。
class A {
}
class B {
}
// B 的實(shí)例繼承 A 的實(shí)例
Object.setPrototypeOf(B.prototype, A.prototype);
// B 繼承 A 的靜態(tài)屬性
Object.setPrototypeOf(B, A);
const b = new B();《對(duì)象的擴(kuò)展》一章給出過(guò) Object.setPrototypeOf 方法的實(shí)現(xiàn)。
Object.setPrototypeOf = function (obj, proto) {
obj.__proto__ = proto;
return obj;
}因此,就得到了上面的結(jié)果。
Object.setPrototypeOf(B.prototype, A.prototype);
// 等同于
B.prototype.__proto__ = A.prototype;
Object.setPrototypeOf(B, A);
// 等同于
B.__proto__ = A;這兩條繼承鏈,可以這樣理解:作為一個(gè)對(duì)象,子類(lèi)( B )的原型( proto 屬性)是父類(lèi)( A );作為一個(gè)構(gòu)造函數(shù),子類(lèi)( B )的原型對(duì)象( prototype 屬性)是父類(lèi)的原型對(duì)象( prototype 屬性)的實(shí)例。
B.prototype = Object.create(A.prototype);
// 等同于
B.prototype.__proto__ = A.prototype;extends 關(guān)鍵字后面可以跟多種類(lèi)型的值。
class B extends A {
}上面代碼的 A ,只要是一個(gè)有 prototype 屬性的函數(shù),就能被 B 繼承。由于函數(shù)都有 prototype 屬性(除了 Function.prototype 函數(shù)),因此 A 可以是任意函數(shù)。
下面,討論兩種情況。第一種,子類(lèi)繼承 Object 類(lèi)。
class A extends Object {
}
A.__proto__ === Object // true
A.prototype.__proto__ === Object.prototype // true這種情況下, A 其實(shí)就是構(gòu)造函數(shù) Object 的復(fù)制, A 的實(shí)例就是 Object 的實(shí)例。
第二種情況,不存在任何繼承。
class A {
}
A.__proto__ === Function.prototype // true
A.prototype.__proto__ === Object.prototype // true這種情況下, A 作為一個(gè)基類(lèi)(即不存在任何繼承),就是一個(gè)普通函數(shù),所以直接繼承 Function.prototype 。但是, A 調(diào)用后返回一個(gè)空對(duì)象(即 Object 實(shí)例),所以 A.prototype.proto 指向構(gòu)造函數(shù)( Object )的 prototype 屬性。
實(shí)例的 proto 屬性
子類(lèi)實(shí)例的 proto 屬性的 proto 屬性,指向父類(lèi)實(shí)例的 proto 屬性。也就是說(shuō),子類(lèi)的原型的原型,是父類(lèi)的原型。
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 ,導(dǎo)致前者原型的原型是后者的原型。
因此,通過(guò)子類(lèi)實(shí)例的 proto.proto 屬性,可以修改父類(lèi)實(shí)例的行為。
p2.__proto__.__proto__.printName = function () {
console.log('Ha');
};
p1.printName() // "Ha"上面代碼在 ColorPoint 的實(shí)例 p2 上向 Point 類(lèi)添加方法,結(jié)果影響到了 Point 的實(shí)例 p1 。
5. 原生構(gòu)造函數(shù)的繼承
原生構(gòu)造函數(shù)是指語(yǔ)言?xún)?nèi)置的構(gòu)造函數(shù),通常用來(lái)生成數(shù)據(jù)結(jié)構(gòu)。ECMAScript 的原生構(gòu)造函數(shù)大致有下面這些。
- Boolean()
- Number()
- String()
- Array()
- Date()
- Function()
- RegExp()
- Error()
- Object()
以前,這些原生構(gòu)造函數(shù)是無(wú)法繼承的,比如,不能自己定義一個(gè) Array 的子類(lèi)。
function MyArray() {
Array.apply(this, arguments);
}
MyArray.prototype = Object.create(Array.prototype, {
constructor: {
value: MyArray,
writable: true,
configurable: true,
enumerable: true
}
});上面代碼定義了一個(gè)繼承 Array 的 MyArray 類(lèi)。但是,這個(gè)類(lèi)的行為與 Array 完全不一致。
var colors = new MyArray();
colors[0] = "red";
colors.length // 0
colors.length = 0;
colors[0] // "red"之所以會(huì)發(fā)生這種情況,是因?yàn)樽宇?lèi)無(wú)法獲得原生構(gòu)造函數(shù)的內(nèi)部屬性,通過(guò) Array.apply() 或者分配給原型對(duì)象都不行。原生構(gòu)造函數(shù)會(huì)忽略 apply 方法傳入的 this ,也就是說(shuō),原生構(gòu)造函數(shù)的 this 無(wú)法綁定,導(dǎo)致拿不到內(nèi)部屬性。
ES5 是先新建子類(lèi)的實(shí)例對(duì)象 this ,再將父類(lèi)的屬性添加到子類(lèi)上,由于父類(lèi)的內(nèi)部屬性無(wú)法獲取,導(dǎo)致無(wú)法繼承原生的構(gòu)造函數(shù)。比如, Array 構(gòu)造函數(shù)有一個(gè)內(nèi)部屬性 [[DefineOwnProperty]] ,用來(lái)定義新屬性時(shí),更新 length 屬性,這個(gè)內(nèi)部屬性無(wú)法在子類(lèi)獲取,導(dǎo)致子類(lèi)的 length 屬性行為不正常。
下面的例子中,我們想讓一個(gè)普通對(duì)象繼承 Error 對(duì)象。
var e = {};
Object.getOwnPropertyNames(Error.call(e))
// [ 'stack' ]
Object.getOwnPropertyNames(e)
// []上面代碼中,我們想通過(guò) Error.call(e) 這種寫(xiě)法,讓普通對(duì)象 e 具有 Error 對(duì)象的實(shí)例屬性。但是, Error.call() 完全忽略傳入的第一個(gè)參數(shù),而是返回一個(gè)新對(duì)象, e 本身沒(méi)有任何變化。這證明了 Error.call(e) 這種寫(xiě)法,無(wú)法繼承原生構(gòu)造函數(shù)。
ES6 允許繼承原生構(gòu)造函數(shù)定義子類(lèi),因?yàn)?ES6 是先新建父類(lèi)的實(shí)例對(duì)象 this ,然后再用子類(lèi)的構(gòu)造函數(shù)修飾 this ,使得父類(lèi)的所有行為都可以繼承。下面是一個(gè)繼承 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上面代碼定義了一個(gè) MyArray 類(lèi),繼承了 Array 構(gòu)造函數(shù),因此就可以從 MyArray 生成數(shù)組的實(shí)例。這意味著,ES6 可以自定義原生數(shù)據(jù)結(jié)構(gòu)(比如 Array 、 String 等)的子類(lèi),這是 ES5 無(wú)法做到的。
上面這個(gè)例子也說(shuō)明, extends 關(guān)鍵字不僅可以用來(lái)繼承類(lèi),還可以用來(lái)繼承原生的構(gòu)造函數(shù)。因此可以在原生數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ)上,定義自己的數(shù)據(jù)結(jié)構(gòu)。下面就是定義了一個(gè)帶版本功能的數(shù)組。
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.history // [[], [1, 2]]
x.revert();
x // [1, 2]上面代碼中, VersionedArray 會(huì)通過(guò) commit 方法,將自己的當(dāng)前狀態(tài)生成一個(gè)版本快照,存入 history 屬性。 revert 方法用來(lái)將數(shù)組重置為最新一次保存的版本。除此之外, VersionedArray 依然是一個(gè)普通數(shù)組,所有原生的數(shù)組方法都可以在它上面調(diào)用。
下面是一個(gè)自定義 Error 子類(lèi)的例子,可以用來(lái)定制報(bào)錯(cuò)時(shí)的行為。
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 的子類(lèi),有一個(gè)行為差異。
class NewObj extends Object{
constructor(){
super(...arguments);
}
}
var o = new NewObj({attr: true});
o.attr === true // false上面代碼中, NewObj 繼承了 Object ,但是無(wú)法通過(guò) super 方法向父類(lèi) Object 傳參。這是因?yàn)?ES6 改變了 Object 構(gòu)造函數(shù)的行為,一旦發(fā)現(xiàn) Object 方法不是通過(guò) new Object() 這種形式調(diào)用,ES6 規(guī)定 Object 構(gòu)造函數(shù)會(huì)忽略參數(shù)。
6. Mixin 模式的實(shí)現(xiàn)
Mixin 指的是多個(gè)對(duì)象合成一個(gè)新的對(duì)象,新對(duì)象具有各個(gè)組成成員的接口。它的最簡(jiǎn)單實(shí)現(xiàn)如下。
const a = {
a: 'a'
};
const b = {
b: 'b'
};
const c = {...a, ...b}; // {a: 'a', b: 'b'}上面代碼中, c 對(duì)象是 a 對(duì)象和 b 對(duì)象的合成,具有兩者的接口。
下面是一個(gè)更完備的實(shí)現(xiàn),將多個(gè)類(lèi)的接口“混入”(mix in)另一個(gè)類(lèi)。
function mix(...mixins) {
class Mix {
constructor() {
for (let mixin of mixins) {
copyProperties(this, new mixin()); // 拷貝實(shí)例屬性
}
}
}
for (let mixin of mixins) {
copyProperties(Mix, mixin); // 拷貝靜態(tài)屬性
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 函數(shù),可以將多個(gè)對(duì)象合成為一個(gè)類(lèi)。使用的時(shí)候,只要繼承這個(gè)類(lèi)即可。
class DistributedEdit extends mix(Loggable, Serializable) {
// ...
} 當(dāng)前名稱(chēng):創(chuàng)新互聯(lián)ES6教程:ES6Class的繼承
本文路徑:http://www.5511xx.com/article/djdgcod.html


咨詢(xún)
建站咨詢(xún)
