JavaScript more rigorous equivalence [translation]_javascript skills

Source: Internet
Author: User
1. Detect Nan
In mathematics, an arbitrary value x is always equal to itself:

x = X

However, this rule does not apply to = = = and NaN:
Copy Code code as follows:

> nan = = Nan
False

As a result, you cannot find this Nan in an array that contains Nan by using the IndexOf method, because the method internally uses = = To determine whether an element is equal to the value specified in the parameter:
Copy Code code as follows:

> [Nan].indexof (Nan)
-1

Translator NOTE: The switch statement is equally
Copy Code code as follows:

Switch (NaN) {
Case Nan:alert (NaN);
}

If you can't use = = = to detect Nan, what should you use? There is a global function isNaN (), but the problem with this function is that it always implicitly converts the value in the parameter to a number, and then returns True when judging many values that are obviously not nan:
Copy Code code as follows:

> isNaN ("foo")
True

Explanation: "foo" was converted to the number Nan.
Copy Code code as follows:

> Number ("foo")
NaN

Another way to detect Nan is to use Nan as the only value that is strictly not equal to itself:
Copy Code code as follows:

function Myisnan (value) {
return value!== value;
}

Another more understandable approach is to check that the value is a numeric type before using isNaN (). This avoids the problem of implicit conversions.
Copy Code code as follows:

function MyIsNaN2 (value) {
return typeof value = = = ' Number ' && isNaN (value);
}

Ecmascript.next will have a new Number.isnan () method, which is a modified version of the isNaN () function.

2. Distinction-0 and +0

This is a rare requirement, but sometimes you do need to differentiate between +0 (positive 0) and-0 (minus 0), in JavaScript, which is two different. but = = cannot be judged:
Copy Code code as follows:

>-0 = +0
True

So what's the difference? in JavaScript. If you divide a positive number by −0, the result is-infinity. If divided by +0, the result is Infinity. These two infinity values can be judged by using = = =:
Copy Code code as follows:

> 1/-0
-infinity

> 1/+0
Infinity

> Infinity = =-infinity
False

As a function, the translator notes
Copy Code code as follows:

function Ispositivezero (zero) {
return 1/zero = = Infinity

}
function Isnegativezero (zero) {
return 1/zero = =-infinity
}


Stricter equality in 3.ecmascript.next: "is" operator

Ecmascript.next will have a " is"The operator, it's the" more strict equality ": it can nan is considered equal to itself, and can distinguish between -0 and +0. There is also a reverse operator called "isnt". For example:
Copy Code code as follows:

> Nan is Nan
True

>-0 isnt +0
True


The current operator can be compensated by the object.is () method . This method can be implemented as follows:

Copy Code code as follows:

object.is = function (x, y) {
if (x = = y) {
x = = 0 => comparison +0 and-0
return x!== 0 | | (1/x = = 1/y);
}

X!== y => returns true only if both X and Y are Nan
return x!== x && y!== y;
};


3.1 Try Object.is ()

If you want to try object.is (), you can use Es6-shim, which can migrate some of the features in Ecmascript.next (ECMAScript 6) to ECMAScript 5.

If you want to use it in a ES3 environment, you have to use Es5-shim

4. For reference
    1. equality in JavaScript: = = = versus = =
    2. ECMAScript.next:the "TXJS" Update by eich
    3. NaN and Infinity in JavaScript
    4. es6-shim–ecmascript 6 functionality on ECMAScript 5
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.