简体   繁体   English

JavaScript 中变量阴影的正确术语是什么?

[英]What is the correct term for variable shadowing in JavaScript?

Below we have an IIFE which (like any function) creates a local scope.下面我们有一个IIFE ,它(像任何函数一样)创建了一个局部作用域。 Inside that scope there is a parseInt function.在该范围内有一个parseInt函数。 Now, since there already is a global function in the browser with that name, the local function will overshadow the global parseInt function - inside the IIFE any call to parseInt will call the local function, and not the global one.现在,由于浏览器中已经有一个具有该名称的全局函数,本地函数将掩盖全局parseInt函数——在 IIFE 中,对parseInt任何调用都将调用本地函数,而不是全局函数。 (The global function can still be referenced with window.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?是否有法律上的(ECMAScript 规范)或事实上的(通用)名称? Overshadowing?遮天蔽日? Overloading?超载?

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) . JavaScript 中的函数只是存储在变量(或属性)中的函数对象,它们遵循与普通变量(或属性)相同的作用域链/解析规则 This explains why the original can still be accessed as window.parseInt as well.这解释了为什么原始文件仍然可以作为window.parseInt访问。 It is the "IIFE" which introduces this new scope (functions are the only way to introduce new scope in JavaScript).是“IIFE”引入了这个新作用域(函数是在 JavaScript 中引入新作用域的唯一方法)。

However, the ECMAScript Specification [5th Edition] does not use the term shadowing , nor can I find a specific replacement term.但是, ECMAScript 规范 [第 5 版]没有使用术语shadowing ,我也找不到特定的替代术语。 (The fundamental shadowing behavior is defined in "10.2.2.1 GetIdentifierReference" and related sections.) (基本的遮蔽行为在“10.2.2.1 GetIdentifierReference”和相关章节中定义。)

It is not overloading and it is not overriding , 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 shadowing didn'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").如果术语阴影不存在来解释这种行为,那么——无论如何,从英语的角度来看——遮蔽(“使无关紧要/无关紧要”)可能比阴影(“将阴影投射/变暗”)更合适.

Happy coding.快乐编码。

If it happened by accident/mistake, you would call it clobbering the original parseInt() .如果它是偶然/错误发生的,你会称它为破坏原始的parseInt()

Otherwise, I believe I saw it referred to shadowing recently here on Stack Overflow.否则,我相信我最近在 Stack Overflow 上看到它提到了阴影

通常称为“阴影”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM