ES6 多层继承

318 阅读1分钟
import a from "a";
import b from "b";

// 深度拷贝
const copyProperties = function(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)
        }
    }
}
// 多层继承
const newClass = function(...data) {
    class newClass {}
    for (let item of data) {
        // 将item方法 原型 拷贝到 newClass类中      
        copyProperties(newClass, item) // 拷贝原型     
        copyProperties(newClass.prototype, item.prototype)
    }
    return newClass
}
class person extends newClass(a, b) {
    constructor() {
        super()
    }
}