便利贴--50{拆解class成多个文件进行引入处理-便于管理一个类多个功能}
使多个class 合并成一个class进行实例化,拥有所有class的实例和原形
js
util.js
function mixinsClass(...mixins) {
class MixinClass {
constructor(arg) {
let i = 0,
back = [];
for (let mixin of mixins) {
let example = new mixin(arg);
back.push(example)
copyProperties(this, example);
}
if (back.length > 0) {
return this;
这里可以选择返回任何的实例
}
}
};
let proto = MixinClass.prototype;
console.log()
for (let mixin of mixins) {
copyProperties(MixinClass, mixin);
copyProperties(proto, mixin.prototype);
}
return MixinClass;
}
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);
}
}
}
export {
mixinsClass,
}
index.js
import {
mixinsClass
} from './util.js'
import initMap from './src/index.js'
import draw from './src/draw.js'
import listener from './src/listener.js'
class bdMap extends mixinsClass(initMap, draw, listener) {}
export default bdMap;