is there a way to access the value of shadowed variable a in the scope of x()
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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.