/** ***************************************** * Created by edonet@163.com * Created on 2021-03-21 14:28:09 ***************************************** */ 'use strict'; /** ***************************************** * 加载依赖 ***************************************** */ import { Extend } from './d'; /** ***************************************** * 代理对象 ***************************************** */ type Proto = Record; type Proxy = Extend; /** ***************************************** * 生成代码对象 ***************************************** */ function proxy(target: T): T; function proxy(target: T, proto?: P): Proxy function proxy(target: T, proto?: P): Proxy { const ob = Object.create(typeof proto !== 'undefined' ? proto : target as Proto); // 不存在属性 if (!target || typeof target !== 'object') { return ob; } // 解析属性 Object.keys(target).forEach(key => { Object.defineProperty(ob, key, { enumerable: true, configurable: true, set(value: unknown) { target[key as keyof T] = value as T[keyof T]; }, get() { return target[key as keyof T]; }, }); }); // 返回结果 return ob; } /** ***************************************** * 抛出接口 ***************************************** */ export { proxy };