Javascript中使用Object.is()判断严格相等
2017-05-11 15:12
459 查看
转载自:
http://www.tuicool.com/articles/eiAfMvV
http://www.zuojj.com/archives/1211.html?utm_source=tuicool&utm_medium=referral
在Javascript中判断相等是很常见的,常用的判断有“==”,“===”,“!=”,“!==”,今天这篇文章我们来学习ES6中的一个方法Object.is(),关于此方法的详细介绍可以戳 这里。
Object.is()方法用来判断两个值是否有相同的值,这个相同的值的判定和“==”,“===”的判断不同,比如:
具体有以下几种情况:
both
both
both
both strings of the same length with the same characters
both the same object
both numbers and
both
both
both
or both non-zero and both not
and both have the same value
常见的几种比较:
Sameness Comparisons
因为Object.is()是ES6规范草案的一部分,现在只有Chrome/FF的部分版本支持,如果你想兼容其它浏览器及低版本,可以使用Polyfill或者 ES6-shim。
http://www.tuicool.com/articles/eiAfMvV
http://www.zuojj.com/archives/1211.html?utm_source=tuicool&utm_medium=referral
在Javascript中判断相等是很常见的,常用的判断有“==”,“===”,“!=”,“!==”,今天这篇文章我们来学习ES6中的一个方法Object.is(),关于此方法的详细介绍可以戳 这里。
Object.is()方法用来判断两个值是否有相同的值,这个相同的值的判定和“==”,“===”的判断不同,比如:
console.log(NaN == NaN); //falseconsole.log(Object.is(NaN, NaN)); //trueconsole.log(+0 == -0); //trueconsole.log(+0 === -0); //trueconsole.log(Object.is(+0, -0)); //false
具体有以下几种情况:
both
undefined
both
null
both
trueor both
false
both strings of the same length with the same characters
both the same object
both numbers and
both
+0
both
-0
both
NaN
or both non-zero and both not
NaN
and both have the same value
常见的几种比较:
x | y | == | === | Object.is |
---|---|---|---|---|
undefined | undefined | true | true | true |
null | null | true | true | true |
true | true | true | true | true |
false | false | true | true | true |
"foo" | "foo" | true | true | true |
{ foo: "bar" } | x | true | true | true |
true | true | true | ||
+0 | -0 | true | true | false |
false | true | false | false | |
"" | false | true | false | false |
"" | true | false | false | |
"0" | true | false | false | |
"17" | true | false | false | |
[1,2] | "1,2" | true | false | false |
new String("foo") | "foo" | true | false | false |
null | undefined | true | false | false |
null | false | false | false | false |
undefined | false | false | false | false |
{ foo: "bar" } | { foo: "bar" } | false | false | false |
new String("foo") | new String("foo") | false | false | false |
null | false | false | false | |
NaN | false | false | false | |
"foo" | NaN | false | false | false |
NaN | NaN | false | false | true |
相关文章推荐
- es6 javascript 的Object.is(),判断两个值相等
- javascript object 相等判断逻辑
- ES6中的Object.is()判断对象是否相等
- Object-C 中拒绝使用==判断两个对象相等
- JavaScript中使用Object.prototype.toString判断是否为数组
- python中对象相等判断(is、==与__dict__的使用)
- WEB前端——使用Javascript判断客户端浏览器类型
- 在使用SMO时,检索IsSystemObject属性将导致性能急剧下降的解决方法
- 用JavaScript 判断用户使用的是 IE6 还是 IE7
- 使用String.IsNullOrEmpty来判断字符串是否为空.
- JavaScript 判断判断某个对象是Object还是一个Array
- 什么时候应该使用==?什么时候应该使用Equals?(如何判断值相等还是引用相等。)
- 在javaScript中使用正则表达试判断输入是否是float类型
- javascript判断用户使用的浏览器
- Object Oriented Programming in JavaScript 在Javascript中使用面向对象的编程
- javascript Object与Function使用
- javascript中使用正则表达式的一例 - 判断页码范围
- jsp中,使用javascript判断radio是否有选中!
- Javascript判断object 的特定类(文章经转者改动)
- 使用javascript判断浏览器版本