Skip to content

Latest commit

 

History

History
188 lines (135 loc) · 6.67 KB

File metadata and controls

188 lines (135 loc) · 6.67 KB

Preview

Javascript Hoist the right way assasin level

You will learn How to beat the Master

Javascript Hoist the right way assasin level

nagisa

Nagisa: What are you doing Koro sensi😱🥴???

koro-sensei

Koro sensei: Playing with him, can you see how he is messed-up and stuck at these?😈😅

//attack level-1==> Calling the function `foo` before its declaration
foo();
function foo() {
  console.log("Tell me matin, what is the output?:-)");
}

nagisa

Nagisa: oh oh, matin, listen to me, remember hoisting definition and tell him.🤠

Javascript Hoist the right way assasin level

developer he/she

developer:a.h.amm, semsemni, id h is becaause in d hoisding, declaration goes do de dop of de scop, so id will run de console😩®

director

SeyyedKhandon: Where ever you see a ®, there will be a detail/explanation/description for that. e.g. aha, sensi, it is because in the hoisting, declaration goes to the top of the scope, so it will run the console

// at-l1==:
function foo() {
  console.log("Tell me matin, what is the output?:-)");
}
foo();
//===================================================

koro-sensei

Koro Sensi: hmmmm...!, How about these two?

// Attack level-2===> Calling the function before its assignment and declaration
foo();
var foo = function () {
  console.log("Easy peasy hah? got stuck?😈");
};
foo();
var foo = function foo() {
  console.log("Seems to be full of yourself, ✌");
};

nagisa

kaede: developer, remember, the rhs and lhs...

Javascript Hoist the right way assasin level

developer he/she

developer: thank you kaede. Sensei, it will hoist the foo declaration, so we have a foo with undefined, then at that assignment it will be a “function”, so we run foo before it has been a function makes it a type error.

// at-l2 ==
var foo;
foo();
foo = function () {
  console.log("Easy peasy hah? got stuck?😈");
};
//============================================
Javascript Hoist the right way assasin level

Level 3 Attacks:

Javascript Hoist the right way assasin level
Javascript Hoist the right way assasin level

koro-sensei

Koro sensei: I'll teach you a lesson, js assassins with combined ones:

foo();
function foo() {
  console.log("Got Confused hah?");
}
function foo() {
  console.log("I'll beat you to the death in this battle...");
}
var foo = function () {
  console.log("I bet you missed, hah?:-, just run away you coward...");
};
foo();
(function () {
  var a = b = 3;
})();

console.log(a);
console.log(b);
Javascript Hoist the right way assasin level

karma

Karma: dont worry guys, We will show him what is assassination:

director

SeyyedKhandon: Karma is a real-deal: https://aparat.com/v/DjG4U

Javascript Hoist the right way assasin level

karma

Karma: just seperate and hoist the declarations to the top, then execute them, So first foo() will print I'll beat you, then the second foo() which is after the assignment will print out the I bet you missed...

function foo() {
  console.log("Got Confused hah?");
}
function foo() {
  console.log("I'll beat you to the death in this battle...");
}
var foo;
foo();
foo = function () {
  console.log("I bet you missed, hah?:-, just run away you coward...");
};
foo();

karma

Karma: For this, first we declared a then we defined b without var , so it will be a global variable and will be acessible outside of IIFE function, then we set the value of b into a, then the IIFE function has been finished, so after that, the only variable which has been declared and defined is b and there is no a, so we will get Uncaught ReferenceError: a is not defined

(function () {
  var a;
  b = 3;
  a = b;
})();

console.log(a);
console.log(b);
Javascript Hoist the right way assasin level

Contribute

  • Your first contribution can be as simple as clicking the star button at the top-right corner of the page.
  • I would greatly appreciate any bug fixes or type/grammatical errors fixes.
  • The mini-series will be in English, but any translation in other languages is very welcomed.
  • We plan to make mini-series videos in Persian, but any help to make the English version, will be greatly appreciated and welcomed.

Some of Current used resources: