JavaScript 中的变量阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5373278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:59:08  来源:igfitidea点击:

Variable shadowing in JavaScript

javascriptscopeiife

提问by ?ime Vidas

Below we have an IIFEwhich (like any function) creates a local scope. Inside that scope there is a parseIntfunction. Now, since there already is a global function in the browser with that name, the local function will overshadow the global parseIntfunction - inside the IIFE any call to parseIntwill call the local function, and not the global one. (The global function can still be referenced with window.parseInt.)

下面我们有一个IIFE,它(像任何函数一样)创建了一个局部作用域。在该范围内有一个parseInt函数。现在,由于浏览器中已经有一个具有该名称的全局parseInt函数,本地函数将掩盖全局函数——在 IIFE 中,任何调用都parseInt将调用本地函数,而不是全局函数。(全局函数仍然可以用 引用window.parseInt。)

parseInt('123', 10); // the browser function is called

(function() {

    function parseInt() { return 'overshadowed'; }

    parseInt('123', 10); // the local function is called

})();

parseInt('123', 10); // the browser function is called

Is there a de jure(ECMAScript spec) or de facto(common) name for this? Overshadowing? Overloading?

是否有法律上的(ECMAScript 规范)或事实上的(通用)名称?遮天蔽日?超载?

回答by

The correct term is [Variable] Shadowing

正确的术语是[Variable] Shadowing

In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope.This outer variable is said to be shadowed...

在计算机编程中,当在特定范围(决策块、方法或内部类)内声明的变量与在外部范围内声明的变量具有相同名称时就会发生变量影子。据说这个外部变量是隐藏的......

Functions in JavaScript are just function-objects stored within variables (or properties) that follow the same scope-chain/resolution rules as normal variables (or properties). This explains why the original can still be accessed as window.parseIntas well. It is the "IIFE" which introduces this new scope (functions are the only way to introduce new scope in JavaScript).

JavaScript 中的函数只是存储在变量(或属性)中的函数对象,它们遵循与普通变量(或属性)相同的作用域链/解析规则。这解释了为什么仍然可以访问原始文件window.parseInt。是“IIFE”引入了这个新作用域(函数是在 JavaScript 中引入新作用域的唯一方法)。

However, the ECMAScript Specification [5th Edition]does not use the term shadowing, nor can I find a specific replacement term. (The fundamental shadowingbehavior is defined in "10.2.2.1 GetIdentifierReference" and related sections.)

但是,ECMAScript 规范 [第 5 版]没有使用术语shadowing,我也找不到特定的替代术语。(基本的遮蔽行为在“10.2.2.1 GetIdentifierReference”和相关章节中定义。)

It is notoverloadingand it is notoverriding, which are entirely different. I have no idea where overshadowing(in this context) originated or how it is supposed to differ from "normal" [variable] shadowing. If the term shadowingdidn't already exist to explain this behavior then -- from an English language viewpoint anyway -- overshadowing("to make insignificant/inconsequential") might be more appropriate than shadowing("to cast shadow on/darken").

超载,它是不是压倒一切,这是完全不同的。我不知道overshadowing(在这种情况下)起源于哪里,也不知道它与“正常” [variable] shadowing有何不同。如果术语阴影不存在来解释这种行为,那么——无论如何,从英语的角度来看——遮蔽(“使无关紧要/无关紧要”)可能比阴影(“将阴影投射/变暗”)更合适.

Happy coding.

快乐编码。

回答by alex

If it happened by accident/mistake, you would call it clobberingthe original parseInt().

如果它是偶然/错误发生的,你会称它为破坏原版parseInt()

Otherwise, I believe I saw it referred to shadowingrecently here on Stack Overflow.

否则,我相信我最近在 Stack Overflow 上看到它提到了阴影

回答by RobG

More commonly called "shadowing".

通常称为“阴影”。