如何证明未定义类型只有一个值?

发布于 2024-11-03 13:50:51 字数 855 浏览 3 评论 0原文

阅读 JavaScript Garden 后,我做了如下笔记,但是否可以证明第 2 点。请验证&也纠正我的其他证明。

  1. 未定义是一种类型。
  2. 未定义类型只有一个 价值。该值未定义。
  3. 有一个全局变量叫 未定义的
  4. 全局未定义变量有 默认值 undefined
  5. 全局未定义变量不是 常量
  6. 未定义不是关键字

证明(在firebug控制台中测试)

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

谢谢http://w3fools.com /#应该做什么

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 console)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-11-10 13:50:53

您可以通过查看语言规范来证明这一点。没有可以在解释器中运行的测试来证明这一点。这样的测试只能证明解释器是否符合规范。规范定义了“未定义”。

语言规范第 8.1 节说

8.1 未定义类型

未定义类型只有一个值,称为
未定义。任何没有出现过的变量
被赋值有值
未定义

并且恰好有一个类型 typeof 将返回 "undefined"。来自规范第 11.4.3 节:

表 20 — typeof 运算符结果

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".

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".
胡大本事 2024-11-10 13:50:53

正确的证明是:

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

它必须是真的,因为您已将未定义的变量值更改为与未定义类型不同的任何值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文