最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Explain If Type(x) is Undefined, return true. If Type(x) is Null, return true - Stack Overflow

programmeradmin3浏览0评论

In JavaScript spec: .htm

11.9.6 The Strict Equality Comparison Algorithm

The parison x === y, where x and y are values, produces true or false. Such a parison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same Number value as y, return true.
    4. If x is +0 and y is -0, return true.
    5. If x is -0 and y is +0, return true.
    6. Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false. NOTE This algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs

What does the bolded section mean? How do you write out some JavaScript to confirm it? I tried alert(typeof(undefined) === 'x'); but it gives me false.

In JavaScript spec: http://www.ecma-international/publications/standards/Ecma-262.htm

11.9.6 The Strict Equality Comparison Algorithm

The parison x === y, where x and y are values, produces true or false. Such a parison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same Number value as y, return true.
    4. If x is +0 and y is -0, return true.
    5. If x is -0 and y is +0, return true.
    6. Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false. NOTE This algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs

What does the bolded section mean? How do you write out some JavaScript to confirm it? I tried alert(typeof(undefined) === 'x'); but it gives me false.

Share Improve this question edited Nov 10, 2011 at 18:35 Jonathon Faust 12.6k4 gold badges52 silver badges63 bronze badges asked Nov 10, 2011 at 18:29 Ray ChengRay Cheng 12.6k16 gold badges78 silver badges139 bronze badges 2
  • Can you fill the blahs in order to give us more context without having to download an external PDF file? – Frédéric Hamidi Commented Nov 10, 2011 at 18:32
  • See: stackoverflow./questions/6031372/undefined-and-null – Diodeus - James MacFarlane Commented Nov 10, 2011 at 18:32
Add a ment  | 

3 Answers 3

Reset to default 6

Before that it says:

where x and y are value

So first, give x and y values.

Then forget "blah", 1 is important. x and y have to be the same type to get past step 1.

Step 2 is "If Type(x) is Undefined, return true.".

There is only one value that gives the undefined type, undefined. Thus, the only way to test step 2 (short of assigning undefined to variables):

alert(undefined === undefined)

… will give true.

Step 3 works in exactly the same way. The only null value is null.

alert(null === null)

A manual implementation the algorithm would start like this:

function equalsequalsequals(x, y)
if (typeof x != typeof y) {
    return false;
} else if (typeof x == "undefined") {
    return true;
} // …

The typeof operator can't tell us if something is null or not, so you can't pletely implement the algorithm without using ===. Since we have ===, however, we don't need to.

My interpretation is one of reading the spec as a formal proof, having the numbers 2 and 3 dependent on the first rule:

  1. If Type(x) is different from Type(y), return false.

Since you are at step 2 and have not returned false, the type of X must be the same as of Y, so the condition it is checking for is:

(null) === (null)

which returns true. The same applies with Undefined

Type() is an internal method, not available in your javascript code. It is not identical to the typeof operator.

x and y refer to the left and right hand operands of ===. They are not strings 'x' and 'y'.

http://es5.github./#x11.9.6

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论