es6提供了Object.js()寂殉,特殊處理了+0和-0,以及NAN和NAN的關(guān)系原在,所以只需要在全等===情況下友扰,判斷x !== 0的情況和 NAN彤叉!== NAN的情況即可。下面是使用es5實(shí)現(xiàn)Object.js():
Object.defineProperty(Object, 'is', {
value: function(x, y) {
if (x === y) {
// 針對(duì)+0 不等于 -0的情況
return x !== 0 || 1 / x === 1 / y;
}
// 針對(duì)NaN的情況
return x !== x && y !== y;
},
configurable: true,
enumerable: false,
writable: true
});