Files
cocos4/cocos/core/data/utils/constget.ts
T
Leslie Leigh (李的序) 284aa38d25 Build time constants (#6256)
* consts

* Bump @cocos/build-engine version

* Fix

* Upload @cocos/build-engine

* Rm scripts/build-engine/.gitignore
2020-03-11 14:05:22 +08:00

24 lines
799 B
TypeScript

import { DEBUG } from 'internal:constants';
/**
* 在调试模式下,将属性的 Get 访问器标记为不可变的。
* 属性必须为 Javascript 原生类型或继承自 ValueType。
* 非调试模式下,此装饰器没有任何效果。
*/
export function constget (target: Object, propertyKey: string | symbol) {
if (!DEBUG) {
return;
}
const propertyDescriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
if (propertyDescriptor && propertyDescriptor.get) {
const rawGet = propertyDescriptor.get;
function constGet (this: any) {
const value = rawGet.call(this);
return value ? Object.freeze(value.clone()) : value;
}
propertyDescriptor.get = constGet;
}
return propertyDescriptor;
}