This is an experimental technology, part of the Harmony (EcmaScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
概述
判断两个值是否真正是同一个值.
Method of Object |
|
|---|---|
| Implemented in | JavaScript 1.8.5+ |
| ECMAScript Edition | ECMAScript 6th Edition |
语法
var isSame = Object.is(value1,value2)
参数
-
value1 - 需要比较的第一个值
-
value1 - 需要比较的第二个值
描述
类似于严格相等===运算符,除了两个特例情况:就是NaN和NaN比较,以及+0和-0比较.
示例
Object.is("foo","foo"); // true
Object.is(window,window); // true
Object.is("foo","bar"); // false
Object.is([],[]); // false
//两个特例
Object.is(0,-0); // false
Object.is(NaN,0/0); // true
兼容性
if (!Object.is) { Object.is = function(v1, v2) { if (v1 === 0 && v2 === 0) return 1 / v1 === 1 / v2; if (v1 !== v1) return v2 !== v2; return v1 === v2; }; }
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | Not supported |
22.0 (22) |
Not supported | Not supported | Not supported |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | Not supported | 22.0 (22) | Not supported | Not supported | Not supported |
Mozilla 开发者网络