In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to judge whether two objects are equal in es6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In es6, you can use the is () method of the Object object to determine whether two objects are equal. This method detects whether the value of two variables is the same value, and determines whether the reference addresses of two objects are the same. The syntax "Object.is (object 1, object 2)"; this method returns a Boolean value, and if it returns true, it means that the two objects are equal.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Two objects of type Object, even if they have the same properties and values, are not considered equal when compared with = or = =. This is because they are compared by reference (location in memory), unlike basic types by value comparison.
Var obj1 = {name: "xiaoming", sex: "male"} var obj2 = {name: "xiaoming", sex: "male"} console.log (obj1 = obj2); / / false
However, ES6 provides a method to determine whether two objects are equal. This method determines whether the reference addresses of two objects are the same.
Object.is (afort b)
The Object.is () method determines whether two values are the same value. The two values are equal if the following conditions are met:
It's all undefined.
It's all null.
All true or false.
Are all strings of the same length and the same characters are arranged in the same order
Are all the same objects (meaning that each object has the same reference)
It's all numbers and
All + 0
All are-0
It's all NaN.
Or both are non-zero and non-NaN and have the same value
Return value: a Boolean type to indicate whether two parameters are the same value.
Example 1:
Let obj1= {a: 1} let obj2 = {a: 1} console.log (Object.is (obj1, obj2)) / / falselet obj3 = obj1console.log (Object.is (obj1, obj3)) / / trueconsole.log (Object.is (obj2, obj3)) / / fals
Example 2:
Object.is ('foo',' foo') / / trueObject.is (window, window) / / trueObject.is ([], []) / / falseObject.is (null, null) / / true
Expand knowledge:
The Object.is () method is different from the = = (en-US) operation. The = = operator casts variables on both sides (if they are not of the same type) before judging equality (the result of this behavior is to judge "" = = false as true), while Object.is does not cast values on both sides.
The Object.is () method is also different from the = = (en-US) operation. The = = operator (including the = = operator) treats the numbers-0 and + 0 as equal, while Number.NaN and NaN are not equal.
This is the end of "how to judge whether two objects are equal in es6". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.