JS judges that objects are equal

Source: Internet
Author: User

I am not very good at writing. I have been in the blog Park for the kind of things that I just don't want to talk about. I tried to record some of my experiences. I have been writing this article for over half an hour, who knows how to save, the session times out, and my stuff doesn't know where to go. At that time, I wanted to die, and I was not interested in blogging. If you encounter problems, you may discuss them with your colleagues and post them to see if there are any solutions. However, this situation is not ideal, I don't know whether the question was not understood by others, or whether I made it clear. The question is like a steamed stuffed bun hitting a dog. Let's go to today's topic.

In JS, the common examples of equality are = (equivalent) and = (constant );
= When the values on both sides are of different types, type conversion and comparison are required first;
===, No type conversion is required. The types may vary.

Example:
VaR A = 3;
VaR B = "3 ";
A = B returns true
A = B returns false
This is the judgment of equal basic types.

What happens if it is an object's judgment?

VaR people = function (ID, name ){

This. ID = ID;

This. Name = Name;

}

VaR A = new people ("3", "Xiao"); var B = new people ("3", "Xiao"); var c = A; console. log ("Object equality judgment:" + (A = B); // false console. log ("Object equality judgment:" + (A = C); // true console. log ("Equal object judgment:" + (B = C); // false
Console. Log ("Object equality judgment:" + (A = C); // true

Objects are transmitted by address. Here I think that their object and Java, C # are equivalent to the object addresses? In Java, we compare whether the values of two objects are equal using the equal method. The final comparison is whether the hashcode of the object is equal. If the values are equal, we can determine whether the values of the two objects are equal. Without such a function in JS, you naturally think of whether you can write a common function for judgment. Naturally, you can think of the following method:

VaR people = function (ID, name) {This. id = ID; this. name = Name;} people. prototype. equal = function (OBJ) {If (OBJ instanceof people) {var result = true; For (K in this) {If (this [k]! = OBJ [k]) {result = false; break;} return result;} else {return false;} var A = new people ("3 ", "Xiao"); var B = new people ("3", "Xiao"); var c = A; console. log ("Object equality judgment a equal B:" + (. equal (B); // true

 

It seems that I can meet my requirements now, but this is only in the most general situation. For example, if I make a simple modification to A or B

A. value = 98; // Add a value to object A. This is obviously not supported in Java objects. However, this is not recommended in JS, but it is not ruled out that some people do not. Console. log ("Object equality judgment a equal B:" + (. equal (B); // false Delete. value; B. value = 98; console. log ("Object equality judgment a equal B:" + (. equal (B); // true

In this case, it may be the easiest way to check whether the two attributes have the same length to solve the problem. This can solve the problem, but it may happen again. Please refer

VaR people = function (ID, name, arr) {This. id = ID; this. name = Name; this. group = arr;} people. prototype. equal = function (OBJ) {If (OBJ instanceof people) {var result = true; For (K in this) {If (this [k]! = OBJ [k]) {result = false; break;} return result;} else {return false;} var A = new people ("3", "Xiao ", [1, 2]); var B = new people ("3", "Xiao", [1, 2]); console. log ("Object equality judgment a equal B:" + (. equal (B); // false

When the object field is not a basic type, but an object, it is not equal. The original value is equal, and the expected result is true. At this time, we may think of another solution, that is, to judge the type.
If it is an object type, or an array (that is, an object type), we will use recursive methods to repeatedly call ourselves for judgment. This also seems to work. But is this a bit complicated? Is there a better idea or solution to this problem?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.