为什么window.undefined在旧的浏览器中是可变的? [英] Why is window.undefined mutable in old browsers?

查看:150
本文介绍了为什么window.undefined在旧的浏览器中是可变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写规范以使未定义可变,因此不利于比较的团队的目的是什么?

What was the intent of the team who wrote the specification to make undefined mutable and hence not good for comparison?

推荐答案

我不知道对此有一个单一的,明确的答案,但以下是一些语言设计方面的问题,这些问题可以部分解释其原理和原理。

I don't know of a singular, definitive answer on this, but here's a look a some of the language design concerns which provide a partial explanation of the mechanics & considerations at play here.

首要的是:类似的值,例如 null true false 保留的单词。这些是JavaScript语言的基本组成部分-与 if for 相似。 undefined 似乎很像 null ,因此可以认为它们在用法上相同是很直观的。最终结果有所不同-在特定方面更像是 NaN

First and foremost: comparable values like null, true and false are reserved words. These are fundamental parts of the JavaScript language - on a similar level to if and for. undefined seems an awful lot like null, so it's reasonably intuitive to think they'd be the same in terms of usage. It ends up being a bit different - more like NaN in this particular regard.

问这个问题时,值得问一下,实际上多少是可变的。几乎所有内容-如果您想覆盖任何标准构造函数或函数,则可以。可以设置 Array Math eval 等。

When asking this question, it's worth asking just how much is, in fact, mutable. Practically everything - if you want to override any standard constructor or function, you probably can. Array, Math, eval etc can all be set.

原始语言中没有机制可以使窗口的属性不变。即使设计人员认为阻止对 undefined 的更改是一个好主意,但除了更改语言中的句法含义外,目前尚无其他方法。

There was no mechanism in the original language to even make a property on the window immutable. Even if the designer thought it would be a good idea to prevent changes to undefined there was as of yet no means to do so other than changing its syntactic meaning within the language.

这些天来,核心语言规范提供了对如何以及为什么可以更改变量的更多控制。可通过配置ECMAScript 5的方式来控制属性的可变性以及变量是否为可写。可以通过 Object.getOwnPropertyDescriptor 函数。我认为在窗口上唯一可能被视为核心JavaScript的不可写值为 undefined Infinity 和< codeNaN (其他值在浏览器中不可写,但是我相信它们几乎都与浏览器本身相关,并且对语言不是根本性的)。规范的第15.1.1节具有细节;我在原始规范

These days though, the core language specification offers much more control over how and why a variable can be changed. ECMAScript 5 added in ways to control how properties are mutable, and whether or not a variable is writable can be configured. This can be seen via the Object.getOwnPropertyDescriptor function. I believe the only non-writable values on window which might be considered "core JavaScript" are undefined, Infinity and NaN (other values are non-writable in a browser, but I believe they are almost all related to the browser itself, and are not fundamental to the language). Section 15.1.1 of the specification has the details on this; I could find nothing similar in the original spec.

这仍然不能完全阻止您覆盖未定义-如果您不在全局范围内,则可以将其视为变量。因此,在全局范围内执行时,这只会打印 undefined -

This still doesn't prevent you entirely from "overriding undefined" - if you're not in the global scope, you can treat it as a variable. So, this will simply print undefined when executed in the global scope -

var undefined = 5;
console.log(undefined);

但这将打印5-

(function() {
    var undefined = 5;
    console.log(undefined);
})();

这根本不会改变值,它只是创建了一个新的本地引用,该变量称为未定义。由于该值是在顶级范围(窗口)上定义的,因此该语言仅阻止分配给该特定对象上的该特定属性。

This doesn't change the value at all, it just creates a new local reference to a variable called "undefined". Since the value is defined on the top-level scope (window), the language only prevents assignment to that specific property on that one specific object.

这篇关于为什么window.undefined在旧的浏览器中是可变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆