es6提供了Object.js(),特殊处理了+0和-0,以及NAN和NAN的关系,所以只需要在全等===情况下,判断x !== 0的情况和 NAN!== NAN的情况即可。下面是使用es5实现Object.js():
Object.defineProperty(Object, 'is', {
value: function(x, y) {
if (x === y) {
// 针对+0 不等于 -0的情况
return x !== 0 || 1 / x === 1 / y;
}
// 针对NaN的情况
return x !== x && y !== y;
},
configurable: true,
enumerable: false,
writable: true
});
网友评论