Why 'NaN' and 'Undefined' are not

2019-01-19 10:56发布

问题:

We can do:

NaN = 'foo' 

as well as

undefined = 'foo'

Why they are not reserved keywords?

Edit 1 (DownVoters):

  1. I think it should be implemented in order to be sure that when we are looking for a number, it is a number :)

  2. If we should use IsNaN() or typeof so why NaN or undefined are needed?

回答1:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN

NaN is a property of the global object.

The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined

undefined is a property of the global object, i.e. it is a variable in global scope.

The initial value of undefined is the primitive value undefined.



回答2:

I cannot tell you why, but undefined and NaN are actually properties of the global object:

15.1.1 Value Properties of the Global Object

15.1.1.1 NaN
The value of NaN is NaN (see 8.5). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
(...)
15.1.1.3 undefined
The value of undefined is undefined (see 8.1). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

There is a difference between the value undefined (NaN) and the corresponding property.

You might notice the [[Writable]]: false. I'm not sure whether this new in ES5 (and might not be adapted by all browsers), but in newer browsers (tested in Firefox 6), assigning a new value to undefined has no effect:

[12:28:15.090] < undefined = "foo"
[12:28:15.093] > "foo"
[12:28:19.882] < undefined
[12:28:19.883] > undefined

So although it seems you can assign a new value, you actually cannot.


Why they are not reserved keywords?

Not sure if there was a specific reason to not make them reserved keywords, but it was decided against it. The language still works. You cannot override these values, so it's fine.

The only way to test whether a number is NaN, is to use isNaN() anyway.



回答3:

I'm speculating now, but the reason why I think NaN and undefined are not keywords is because you generally don't assign these values to variables.

var x = undefined; // doesn't make sense, use null!
var y = NaN; // You can't do much with this variable!

undefined basically means uninitialized, and if you want to make it clear that the value is unknown you use null. So undefined usually means not-initialized or the result of JavaScript code gone wrong.

NaN Is rarely assigned manually, simply because you can't do much with this value. It is usually the result of a calculation gone wrong. The makers of JavaScript probably didn't want to give this value the visibility of primitive values.

Also, NaN is also present in other languages and it isn't used as a keyword there either. For example: In C# NaN is represented by Double.NaN, since you don't make a distinction between floating point and integer values in JavaScript, I'm guessing that's why they put NaN with the Global Identifiers!

I hope this clears things up!



回答4:

NaN is not a keyword, but is rather a built-in property of the global object, and as such may be replaced (like undefined, but unlike the keyword this or the literals true, false, and null).

You can test if a value is NaN with the isNaN() function. Moreover NaN is defined to be unequal to everything, including itself.

Or in a nut shell you can say that:

NaN is the value returned when you try to treat something that is not a number as a number. For instance the results of 7 times "abc" is not a number. The old form of it is Number.NaN You can test for not-a-number values with the isNaN() function.

Hope this helps.