Javascript Object comparison implementation code

Source: Internet
Author: User
Tags string to number

Javascript Object comparison
Comparison OPERATOR: = ,! =, = ,! ==, >=, <=, >, <
= Always try to compare them straight, if the types are different, always try to convert.
=== Comparison same, comparison without conversion
= If it is a basic type (string, number, boolean), compare their values,
Var a = "123 ";
Var B = 123;
Then (a = B) = true;
(A = B) = false;
If it is object, array, function type, compare their reference. Only when their reference is equal is true.
Function Point (x, y ){
This. x = x;
This. y = y;
};

Point. prototype. toString = function (){
Alert ("in toString ");
Return "x =" + this. x + "y =" + this. y;
};

Point. prototype. valueOf = function (){
Alert ("in valueOf ");
Return this. x + this. y;
};
Var pa = new Point (1, 1 );
Var pb = new Point (1, 1 );
Var pc = pa;
Then: pa! = Pb;
Pa! = Pb;
Pa = pc;
Pa === pc;

Var arr1 = [1, 2, 3];
Var arr2 = [1, 2, 3];
Arr1! = Arr2, arr1! = Arr2


I have to say 0, false, null, undefined.
Var t1 = 0;
Var t2 = false;
Var t3 = null;
Var t4;
Then: t1 = t2; t1! = T2;
T1! = T3; t1! = T3;
T1! = T4; t1! = T4;
T2! = T3; t2! = T3;
T2! = T4; t2! = T4;
T3 = t4; t3! = T4;


If an object is compared with a basic type, the valueOf of object is called first, and the toString of the object is compared with the basic type.
If it is compared with boolean, convert true to 1, false to 0, and then compare.
Var pa = new Point (1, 1 );
Alert (pa = 2); "in valueOf" is output, and "true" is output ";
If Point. prototype. valueOf is blocked, "in toString" is output, and "false" is output ";
Var pa = new Point (1, 0 );
Then pa = true;
Relational operators >=, <=, >,<
If both sides are numbers or can be converted to numbers, compare the numbers.
If both sides are strings, or can be converted to string, the string is compared.
If one side can be converted to string and the other side can be converted to number, then try to convert string to number and then compare. If the string cannot be converted to number, it is NaN and false is returned.
If an object is involved in the comparison, it always tries to convert the object to number or string for comparison.
The following is an interesting example:
Function Point (x, y ){
This. x = x;
This. y = y;
};

 

Point. prototype. toString = function (){
Alert ("in toString ");
Return "x =" + this. x + "y =" + this. y;
};

Point. prototype. valueOf = function (){
Alert ("in valueOf ");
Return this. x + this. y;
};
Var pa = new Point (1, 1 );
Var pb = new Point (1, 1 );
Then(Pa = pb) = false;
(Pa> pb) = false;
(Pa <pb) = false;
However:
(Pa> = pb) = true;
(Pa <= pb) = true;

Related Article

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.