1 minute read

Tags: , ,

If you cannot explain these concepts to another programmer, then you are not a Web Developer

開票看歸看!

力宏歸力宏!

該做的事還是要堅持阿!!

GO GO GO!

:heart::heart::heart::heart::heart:

Hoisting

MDN:

JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.

Function hoisting

  • One of the advantages of hoisting is that it lets you use a function before you declare it in your code.
yutingGoGo('ya~');

function yutingGoGo(words) {
    console.log("yuting say: " + words);
    // yuting say: ya~
}
function yutingGoGo(words) {
    console.log("yuting say: " + words);
    // yuting say: ya~
}

yutingGoGo('ya~');

Variable hoisting

  • However JavaScript only hoists declarations, not initializations! This means that initialization doesn’t happen until the associated line of code is executed, even if the variable was originally initialized then declared, or declared and initialized in the same line.

  • Until that point in the execution is reached the variable has its default initialization (undefined for a variable declared using var, otherwise uninitialized).

console.log(tim); // return undefined (from var declaration)
var tim; // declaration
tim = 'yuting'; // initialization
console.log(tim); // return yuting
console.log(tim); // return undefined (from var declaration)
var tim = 'yuting'; // declaration and initialization
console.log(tim); // return yuting
  • If we forget the declaration altogether (and only initialize the value) the variable isn’t hoisted. Trying to read the variable before it is initialized results in ReferenceError exception.
console.log(fourNos);
// Uncaught ReferenceError: a is not defined
//   at <anonymous>:1:13
fourNos = '4 nos';
  • let and const hoisting

    • Variables declared with let and const are also hoisted but, unlike var, are not initialized with a default value. An exception will be thrown if a variable declared with let or const is read before it is initialized.
console.log(noNoNoNo);
//  Uncaught ReferenceError: noNoNoNo is not defined
//     at <anonymous>:1:13 
let noNoNoNo = '4nos';