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

查看:58
本文介绍了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

是否有 de jure (ECMAScript规范)或 de facto (通用)名称?遮盖?超载?

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 ,也找不到特定的替代术语. (基本的 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 (在这种情况下)起源于何处,或者与正常" [可变]阴影有何不同.如果不存在术语 shadowing 来解释这种行为,那么-从英语的角度来看- overshadowing (使无关紧要/无关紧要")可能是比 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天全站免登陆