Files
cocos4/cocos/core/data/utils/constget.ts
T
Leslie Leigh 44f1669fa6 add readOnly decorator (#4626)
* update api document for ILerpable

* add readOnly decorator

* rename readOnly to constget

* doc
2019-06-17 12:00:06 +08:00

23 lines
758 B
TypeScript

/**
* 在调试模式下,将属性的 Get 访问器标记为不可变的。
* 属性必须为 Javascript 原生类型或继承自 ValueType。
* 非调试模式下,此装饰器没有任何效果。
*/
export function constget (target: Object, propertyKey: string | symbol) {
if (!CC_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;
}