Javascript - 为什么全局变量隐藏在整个(body的)函数中? [英] Javascript - Why are global variables hidden throughout the (body of the) function?
问题描述
我理解它是如何工作的(局部函数和全局函数之间的差异),但我并没有完全理解隐藏函数中全局变量的原理,而局部变量尚未定义或初始化。 p>
var scope =global;
函数checkscope(){
console.log(scope);
}
//返回>> undefined
我正在阅读 2页后,在第56页,这本书说: 在非嵌套函数中,作用域链由两个对象组成。第一个是定义函数参数和局部变量的对象,第二个是全局对象。 这不就是说全局变量,全局对象的属性是否在非嵌套函数的作用域链中?这与2页之前的内容不矛盾吗? 我不是最擅长表达自己的人,所以我希望你们明白,而且英语不是我的母语,所以我很抱歉,如果这只是一个误解我的部分文字的含义。 根据评论,代码有问题: 这会记录 由于 本地 注意:如果外部作用域是全局作用域,那么在浏览器环境中,仍然可以访问全局 正如您所见,在全局范围中声明的变量成为 当外部作用域是非全局作用域时,这个技巧并不是非常有用,除非重命名,否则 I understand how it works (the disparities between local and global functions), but I don’t quite get the rationale behind hiding the global variables in a functions while a "local" variable is not yet defined or initialized. I’m reading "Javascript: The Definitive Guide (6th edition)" and I’m talking about chapter 3.10 here. (page 54 to be exact). 2 pages later, at page 56 the book says: "In a non-nested function, the scope chain consists of two objects. The first is the object that defines the functions parameters and local variables, and the second one is the global object." Wouldn’t that mean that global variables, "properties" of the global object are in the scope chain of a non-nested function? Doesn’t that contradict what was said 2 pages earlier? I’m not the best at verbalizing myself, so I hope you guys understand, furthermore, English is not my native language so I apologize if this is just a misunderstanding of the meaning of the text on my part. According to the comments, the code in question is: This will log As The local Side-note: if the outer scope in question is the global one, inside a browser environment, you can still access the global As you can see, variables declared in the global scope become properties of the This trick is not very useful when the outer scope is non-global, the 这篇关于Javascript - 为什么全局变量隐藏在整个(body的)函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
var scope =global;
函数checkscope(){
console.log(scope);
var scope;
console.log(scope);
}
undefined
两次,如 var
声明(以及函数声明)都是挂起到当前范围的顶部。上面的脚本解释为:
var scope =global;
函数checkscope(){
var scope; //声明被提升到作用域顶部
console.log(scope);
console.log(scope);
checkscope
has本地范围
变量,本地范围
变量阴影外部范围的范围
变量。
范围
变量没有分配给它的值,这相当于JavaScript的本地 undefined
值。
作用域
变量通过引用全局对象( window
):
var scope =global;
函数checkscope(){
var scope; //声明被提升到作用域顶部
console.log(scope); //未定义
console.log(window.scope); //global
}
窗口
对象的属性。
范围
变量将保持阴影。据我所知,没有标准化的方式来访问父作用域执行上下文的阴影属性(变量/函数)。var scope = "global";
function checkscope() {
console.log(scope);
}
//this returns >> undefined
var scope = "global";
function checkscope() {
console.log(scope);
var scope;
console.log(scope);
}
undefined
twice, as var
declarations (as well as function declarations) are hoisted up to the top of the current scope. The script above is interpreted as:var scope = "global";
function checkscope() {
var scope; //declaration hoisted up to top of scope
console.log(scope);
console.log(scope);
}
checkscope
has a local scope
variable, the local scope
variable shadows the outer scope's scope
variable.scope
variable has no value assigned to it, which is equivalent to JavaScript's native undefined
value.
scope
variable by referencing the global object (window
):var scope = "global";
function checkscope() {
var scope; //declaration hoisted up to top of scope
console.log(scope); //undefined
console.log(window.scope); //"global"
}
window
object.scope
variable will remain shadowed unless you rename it. There is no standardized way to access shadowed properties (variables/functions) of parent scopes' execution contexts as far as I know.