开发者

How can prove undefined type has only one value?

After reading JavaScript Garden, I took notes as below but is it possible prove number 2. Please validate & correct my other proofs also.

  1. undefined is a type.
  2. undefined type has only one value. that values is undefined.
  3. There is a global variable called undefined
  4. global undefined variable has default value undefined
  5. global undefined variable is not a constant
  6. undefined is not a keyword

Proof (tested in firebug consol开发者_运维技巧e)

1. typeof undefined \\undefined 
 2. Need proof
 3. var name = undefined;  \\ global
    variable undefined is assigned to
    name
 4. alert(name); \\ undefined, that
    means undefined global variable has
    value undefined
 5. undefined = 'rajakvk';  \\ we can
    change value
 6. var undefined = 'rajakvk';  \\ no
    syntax error

var if = 'rajakvk';  \\ SyntaxError: missing variable name

Thanks http://w3fools.com/#what-should-be-done


You can prove it by looking at the language spec. There is no test that you can run in an interpreter that will prove it. Such a test would only be evidence that the interpreter is or is not conforming with the spec. The spec is what defines "undefined."

Section 8.1 of the language spec says

8.1 The Undefined Type

The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

And there is exactly one tyoe for which typeof will return "undefined". From section 11.4.3 of the spec:

Table 20 — typeof Operator Results

Type of val                                          Result
Undefined                                            "undefined"
Null                                                 "object"
Boolean                                              "boolean"
Number                                               "number"
String                                               "string"
Object (native and does not implement [[Call]])      "object"
Object (native or host and does implement [[Call]])  "function"
Object (host and does not implement [[Call]])        Implementation-defined except may not be "undefined", "boolean", "number", or "string".


The correct proof is:

undefined = 1;
alert(typeof undefined !== 'undefined'); // need to be true

It need to be true, because you've changed the undefined variable value to anything different from undefined type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜