容器类
import Image from '../image'
import Canvas from '../canvas'
// 依赖容器
export default class DocumentContainer {
/**
* 容器构建类
*/
constructor() {
this.container = new Map()
// 获得对象原型[prototype]
let obj = Object.getPrototypeOf(this)
// 获取原型key值
let obj_key = Reflect.ownKeys(obj)
// 遍历过滤 constructor 把其他方法存入 map
for (let i = 0; i < obj_key.length; i++) {
obj_key[i] !== 'constructor' && this.container.set(obj_key[i].toLocaleLowerCase(), obj[obj_key[i]])
}
// 返回 map
return this.container
}
/**
* 依赖调用
* @returns {*}
* @constructor
*/
Image() {
return Image()
}
/**
* 依赖调用
* @returns {*}
* @constructor
*/
Canvas() {
return Canvas()
}
}
使用容器类
// document 模拟
// export const document = new Proxy({}, {})
import DocumentContainer from '../Api/ApiContainer/documentContainer'
class _document {
/**
* 构造函数
* 获取依赖容器 new DocumentContainer()
*/
constructor() {
this._function = new DocumentContainer()
}
/**
* e 必须是小写字符串.
* 根据 e 返回容器中的方法()
* 不存在返回 false
* @param e
* @returns {*}
*/
createElement(e) {
// console.log(this._function[e])
if (this._function.get(e)) {
return this._function.get(e)()
}
console.error('容器中不存在的方法!')
return false
}
}
export default new Proxy(new _document, {
/**
* 拦截实现
*/
})