JavaScript 中变量阴影的正确术语是什么? [英] What is the correct term for variable shadowing in JavaScript?

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

问题描述

下面我们有一个 IIFE(像任何函数)创建一个局部作用域.在该范围内有一个 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

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

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 尚不存在来解释这种行为,那么 - 从英语语言的角度来看 - overshadowing(使无关紧要/无关紧要")可能是比阴影(投下阴影/变暗")更合适.

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天全站免登陆