1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- /**
- * 防内存调试变量数字类型
- */
- 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);}
|