简体   繁体   English

有没有办法访问阴影变量?

[英]Is there a way to access a shadowed variable?

is there a way to access the value of shadowed variable a in the scope of x()有没有办法访问 x() 的 scope 中的阴影变量 a 的值

 function x () { a = 1; function foo() { a = 2; console.log(a); } foo(); }; x(); console.log(window.a);

Read this You Don't Know JS the Scope & Closures part, its well explaind阅读这个你不知道的 JS Scope 和闭包部分,它很好解释

Try this:尝试这个:

 function x () { a = 1; function foo() { /* when you write a = 2, you are assining 2 to the global a */ var a = 2; console.log("global a", window.a); console.log("local a", a); } foo(); }; x(); console.log(window.a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM