Javascript - 为什么全局变量隐藏在整个(body的)函数中? [英] Javascript - Why are global variables hidden throughout the (body of the) function?

查看:128
本文介绍了Javascript - 为什么全局变量隐藏在整个(body的)函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解它是如何工作的(局部函数和全局函数之间的差异),但我并没有完全理解隐藏函数中全局变量的原理,而局部变量尚未定义或初始化。 p>

  var scope =global; 
函数checkscope(){
console.log(scope);
}
//返回>> undefined

我正在阅读:权威指南(第6版)而我在这里讨论第3.10章。 (确切地说,第54页)。

2页后,在第56页,这本书说:


在非嵌套函数中,作用域链由两个对象组成。第一个是定义函数参数和局部变量的对象,第二个是全局对象。

这不就是说全局变量,全局对象的属性是否在非嵌套函数的作用域链中?这与2页之前的内容不矛盾吗?



我不是最擅长表达自己的人,所以我希望你们明白,而且英语不是我的母语,所以我很抱歉,如果这只是一个误解我的部分文字的含义。

解决方案

根据评论,代码有问题:

  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
}

正如您所见,在全局范围中声明的变量成为窗口对象的属性。



当外部作用域是非全局作用域时,这个技巧并不是非常有用,除非重命名,否则范围变量将保持阴影。据我所知,没有标准化的方式来访问父作用域执行上下文的阴影属性(变量/函数)。


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.

var scope = "global";
function checkscope() {
    console.log(scope);
}
//this returns >> undefined

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:

var scope = "global";
function checkscope() {
    console.log(scope);
    var scope;
    console.log(scope);
}

This will log 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);
}

As checkscope has a local scope variable, the local scope variable shadows the outer scope's scope variable.

The local scope variable has no value assigned to it, which is equivalent to JavaScript's native undefined value.


Side-note: if the outer scope in question is the global one, inside a browser environment, you can still access the global 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"
}

As you can see, variables declared in the global scope become properties of the window object.

This trick is not very useful when the outer scope is non-global, the 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.

这篇关于Javascript - 为什么全局变量隐藏在整个(body的)函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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