JavaScript中的变量阴影 [英] Variable shadowing in JavaScript

查看:150
本文介绍了JavaScript中的变量阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below we have an IIFE which (like any function) creates a local scope. Inside that scope there is a parseInt function. 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. (The global function can still be referenced with 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

是否存在 de jure (ECMAScript spec)或事实上的(常见)名称?掩盖?超载?

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

推荐答案

正确的术语是 [可变]阴影



在计算机编程中,变量阴影当在某个范围内声明的变量(决策块,方法或内部类)与在外部范围内声明的变量具有相同的名称时发生。这个外部变量被称为阴影...

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


JavaScript中的函数只是存储在遵循相同的变量(或属性)中的函数对象范围链/解决规则作为正常变量(或属性)。这解释了原因仍然可以作为 window.parseInt 访问。引入这个新范围的是IIFE(函数是在JavaScript中引入新范围的唯一方法)。

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.parseInt as well. It is the "IIFE" which introduces this new scope (functions are the only way to introduce new scope in JavaScript).

然而, ECMAScript规范[第5版] 不使用术语 shadowing ,我也找不到具体的替换术语。 (基本的阴影行为在10.2.2.1 GetIdentifierReference及相关章节中定义。)

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

重载并且 覆盖,这完全不同。我不知道黯然失色(在此上下文中)源自哪里或者它应该如何与正常 [变量]阴影不同。如果 shadowing 一词尚不存在以解释这种行为,那么 - 无论如何从英语语言的观点来看 - 黯然失色(使得无关紧要/无关紧要)可能是比阴影更合适(投影阴影/变暗)。

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

快乐编码。

这篇关于JavaScript中的变量阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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