如何证明未定义类型只有一个值?
阅读 JavaScript Garden 后,我做了如下笔记,但是否可以证明第 2 点。请验证&也纠正我的其他证明。
- 未定义是一种类型。
- 未定义类型只有一个 价值。该值未定义。
- 有一个全局变量叫 未定义的
- 全局未定义变量有 默认值 undefined
- 全局未定义变量不是 常量
- 未定义不是关键字
证明(在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
After reading JavaScript Garden, I took notes as below but is it possible prove number 2. Please validate & correct my other proofs also.
- undefined is a type.
- undefined type has only one
value. that values is undefined. - There is a global variable called
undefined - global undefined variable has
default value undefined - global undefined variable is not
a constant - 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过查看语言规范来证明这一点。没有可以在解释器中运行的测试来证明这一点。这样的测试只能证明解释器是否符合规范。规范定义了“未定义”。
语言规范第 8.1 节说
并且恰好有一个类型
typeof
将返回"undefined"
。来自规范第 11.4.3 节:表 20 — typeof 运算符结果
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
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
正确的证明是:
它必须是真的,因为您已将未定义的变量值更改为与未定义类型不同的任何值。
The correct proof is:
It need to be true, because you've changed the undefined variable value to anything different from undefined type.