JavaScript Object comparison implements code _javascript techniques

Source: Internet
Author: User
Tags string to number

JavaScript Object Comparisons
Comparison character:==,!=,===,!==,>=,<=,>,<
= = always try to compare them straight, if the type is not the same, always try to convert.
= = = More identity, no conversion on the comparison
= = If it is the base 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 if 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 to a basic type, the valueof of the objects is called first, and then the ToString of the object is compared to the base type.
If it's a Boolean comparison, first convert true to 1,false to 0 and then compare.
var pa = new Point (1,1);
Alert (pa==2), output "in valueof", and then output "true";
If the Point.prototype.valueOf is shielded, the output "in toString" and then the output "false";
var pa = new Point (1,0);
Then pa==true;
Relational operators >=,<=,>,<
If both sides are numbers, or can be converted to numbers, compare numbers.
If both sides are string, or can be converted to string, the string is compared.
If one side can be converted to a string and the other is a number, then try to convert string to number and then Nan, which returns False if string cannot be converted to number.
If an object participates in a comparison, you always try to convert the object to number or string for comparison.
Here's 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;
but:
(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.