简体   繁体   中英

JavaScript LHS and RHS lookup error

I was reading the Scopes and Closure of You don't know JS book by Kyle Simpson, specifically this topic Error.

function foo(a) {
  console.log( a + b );
  b = a;
} 
foo( 2 );

This example results in reference error. In the book, it said that "b" is not found in the scope. Can anyone help me realize why "b" is not found in that scope?

Assignments are not hoisted in JavaScript so when you run
console.log( a + b );
b has not been defined and so is not found in the scope.

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.

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