/** * 防内存调试变量数字类型 */ class SafeVarNumber { private cache: number = 0; private scale: number = 2; private map: Object = {}; private idx: number = 0; constructor(num: number = 0) { this.value = num; } /**数值 */ get value() { if (Math.abs(this.map[this.idx] * this.scale - this.cache) > 0.000001) { this.map[this.idx] = this.cache / this.scale; } return this.map[this.idx]; } set value(num: number) { delete this.map[this.idx]; this.idx = Math.floor(Math.random() * 256); this.map[this.idx] = num; this.scale = Math.floor(Math.random() * 2) + 2; this.cache = num * this.scale; } valueOf() { return this.value; } toString() { return this.value.toString(); } clone() { return new SafeVarNumber(this.value); } } /** * 防内存调试常量数字类型 */ class SafeConstNumber { private cache: number = 0; private scale: number = 2; private map: Object = {}; private idx: number = 0; constructor(num: number = 0) { this.idx = Math.floor(Math.random() * 256); this.map[this.idx] = num; this.scale = Math.floor(Math.random() * 2) + 2; this.cache = num * this.scale; } /**数值 */ get value() { let num = this.map[this.idx]; if (Math.abs(num * this.scale - this.cache) > 0.000001) { num = this.cache / this.scale; } delete this.map[this.idx]; this.idx = Math.floor(Math.random() * 256); this.map[this.idx] = num; return this.map[this.idx]; } valueOf() { return this.value; } toString() { return this.value.toString(); } clone() { return new SafeConstNumber(this.value); } } export function get_new_safe_number(num:number):SafeVarNumber { return new SafeVarNumber(num);} export function get_new_safe_const_number(num:number):SafeConstNumber { return new SafeConstNumber(num);}