ES6对数值对象Number进行了扩展,
// ES实现Number.isFinite()
(function (global) {var global_isFinite = global.isFinite;Object.defineProperty(Number, 'isFinite', {value: function isFinite(value) {return typeof value === 'number' && 'global_isFinite(value);},configurable: true,enumberable: false,writable: true});
}) (this);
// ES5实现Number.isNaN()
(function (global) {var global_isNaN = global.isNaN;Object.defineProperty(Number, 'isNaN', {value: function isNaN(value) {return typeof value === 'number' && global_isNaN(value);},configurable: true,enumerable: false,writable: true});
}) (this);
// ES5实现Number.isInterger()
(function (global) {var floor = Math.floor,isFinite = global.isFinite;Object.definePropery(Number, 'isInteger', {value: function isInteger(value) {return typeof value === 'number' &&isFinite(value) &&floor(value) === value;},configurable: true,enumerable: false,writable: true});
}) (this)
// ES5实现Number.isSafeInteger
var MIN_SAFE_INTEGER = -Math.pow(2, 53);
var MAX_SAFE_INTEGER = Math.pow(2, 53);
Number.isSafeInteger = function (n) {return (typeof n === 'number' &&Math.round(n) === n &&Number.MIN_SAFE_INTEGER <= n &&n <= Number.MAX_SAFE_INTEGER);
}
// Math.trunc: 除去一个数的小数部分
Math.trunc = Math.trunc || function (x) {return x < 0 ? Math.ceil(x) : Math.floor(x);
};
// Math.sign:判断一个数是正数、负数,还是零,或非数字
Math.sign = Math.sign || function (x) {x = +x; // 转换成一个数if (x === 0 || isNaN(x)) {return x;}return x > 0 ? 1: -1;
};
// Math.cbrt: 计算一个数的立方根
Math.cbrt = Math.cbrt || function (x) {var y = Math.pow(Math.abs(x), 1/3);return x < 0 ? -y : y;
};
参考 《ES6 标准入门》P86~P96