Thuta Learning
JavaScript
AdvancedProgrammingအခြေခံ

Hoisting

JavaScriptသင်ခန်းစာ 3020 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Hoisting က code ကို ရွှေ့တာ မဟုတ်ဘဲ creation phase မှာ declaration တွေကို ကြိုမှတ်ပုံတင်တာ ဖြစ်ကြောင်း ရှင်းပြနိုင်ဖို့
  • var ကို ကြိုဖတ်ရင် undefined ရပြီး let/const ကို ကြိုဖတ်ရင် ReferenceError ရကြောင်း ခန့်မှန်းနိုင်ဖို့
  • Function declaration နဲ့ function expression ကို ကြိုခေါ်ရင် ရလဒ် ဘာကြောင့် ကွာလဲ ရှင်းပြနိုင်ဖို့
  • Temporal dead zone (TDZ) ကြောင့် ဖြစ်တဲ့ shadowing bug ကို ရှာတွေ့နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

အစည်းအဝေး မစခင် ဥက္ကဋ္ဌက တက်ရောက်သူ စာရင်းကို အရင် ဖတ်တယ်လို့ စဉ်းစားကြည့်ပါ။ စာရင်းထဲမှာ နာမည်ရှိတဲ့သူတွေကို အစည်းအဝေး ခန်းမထဲ ရောက်မလာသေးလည်း "ရှိတယ်" လို့ သိထားပါတယ်။ JavaScript ကလည်း scope တစ်ခုကို run မလုပ်ခင် အဲဒီထဲက declaration တွေ (var, let, const, function, class) ကို အရင် စာရင်းသွင်းထားပါတယ်။ ဒါကို hoisting လို့ ခေါ်ပါတယ်။

ဒါပေမယ့် စာရင်းထဲ ရှိတာနဲ့ အသင့်ဖြစ်နေတာ မတူပါဘူး။ Function declaration က အပြည့်အစုံ အသင့်ဖြစ်ပြီး var က undefined value နဲ့ ထိုင်စောင့်ပါတယ်။ let နဲ့ const ကတော့ သူ့ line ကို မရောက်မချင်း ထိလို့ မရပါဘူး။

Problem → Why → Solution

ပြဿနာ: error မပေါ်ဘဲ ဈေးနှုန်း NaN ဖြစ်သွားတယ်

အောက်က code မှာ developer တစ်ယောက်က discount ကို တွက်တဲ့ line ကို ဖိုင်ရဲ့ အပေါ်ပိုင်းကို ရွှေ့လိုက်ပါတယ်။ discountRate ကို var နဲ့ ကြေညာထားလို့ JavaScript က error မပြဘဲ undefined ကို သုံးပြီး ဈေးနှုန်း NaN ဖြစ်သွားပါတယ်။ Error message မရှိလို့ ဒီ NaN က checkout page ထိ ရောက်သွားနိုင်ပါတယ်။

checkout.js
const price = 200;
const finalPrice = price - price * discountRate; // runs first

var discountRate = 0.1; // declared later in the file

console.log(finalPrice); // NaN — no error, just a wrong number

ဘာကြောင့် error မပေါ်တာလဲ

var discountRate ကို hoist လုပ်ထားလို့ variable က file အစကတည်းက ရှိနေပြီး value က undefined ပါ။ 200 * undefined က NaN ဖြစ်ပြီး NaN ကို နုတ်ရင်လည်း NaN ပါပဲ။ let ဒါမှမဟုတ် const နဲ့ ကြေညာထားရင်တော့ line 2 မှာ "Cannot access 'discountRate' before initialization" ReferenceError တက်ပြီး bug ကို ချက်ချင်း မြင်ရပါမယ်။

Core concept: creation phase နဲ့ execution phase

Hoisting ကို "declaration တွေကို scope ရဲ့ အပေါ်ဆုံးကို ရွှေ့တယ်" လို့ မကြာခဏ ရှင်းပြကြပေမယ့် တကယ်တော့ code တစ်ကြောင်းမှ မရွှေ့ပါဘူး။ Function တစ်ခု (ဒါမှမဟုတ် script, module, block တစ်ခု) ကို run တဲ့အခါ engine က အဆင့်နှစ်ဆင့်နဲ့ အလုပ်လုပ်ပါတယ်။ Creation phase မှာ scope ထဲက declaration အားလုံးအတွက် binding (နာမည်နဲ့ value ချိတ်ထားတဲ့ နေရာ) တွေ ဖန်တီးပြီး execution phase မှာမှ code ကို အပေါ်ကနေ အောက်ကို တစ်ကြောင်းချင်း run ပါတယ်။

Declaration အမျိုးအစားအလိုက် creation phase မှာ ရတဲ့ အခြေအနေ မတူပါဘူး။ Function declaration ကို function body အပြည့်နဲ့ binding လုပ်လို့ ကြေညာတဲ့ line မတိုင်ခင် ခေါ်လို့ ရပါတယ်။ var ကို undefined နဲ့ initialize လုပ်လို့ ကြိုဖတ်ရင် error မတက်ဘဲ undefined ရပါတယ်။ let, const နဲ့ class ကတော့ binding ရှိပေမယ့် initialize မလုပ်ရသေးပါဘူး။ Scope စတဲ့နေရာကနေ declaration line ထိ ကြားကာလကို temporal dead zone (TDZ) လို့ ခေါ်ပြီး အဲဒီအတွင်း ဖတ်ရင်ရော ရေးရင်ပါ ReferenceError တက်ပါတယ်။

DeclarationLine မတိုင်ခင် သုံးရင်Scope
function name() {}အလုပ်လုပ်တယ် (body အပြည့်)Function / block
var xundefinedFunction
let x / const xReferenceError (TDZ)Block
class Name {}ReferenceError (TDZ)Block
var f = function () {}TypeError: f is not a functionFunction

Function expression နဲ့ arrow function တွေက variable ထဲ assign လုပ်ထားတဲ့ value တွေပါ။ ဒါကြောင့် hoist လုပ်တာက variable ပဲ ဖြစ်ပြီး function value ကတော့ assign လုပ်တဲ့ line ရောက်မှ ဝင်လာပါတယ်။ var နဲ့ ဆိုရင် undefined ကို ခေါ်လို့ TypeError ရပြီး const နဲ့ ဆိုရင် TDZ ကြောင့် ReferenceError ရပါတယ်။ Hoisting ကို အားကိုးပြီး code ရေးတာထက် "သုံးမယ့်နေရာ မတိုင်ခင် ကြေညာပါ" ဆိုတဲ့ စည်းကမ်းက ဖတ်ရလွယ်ပြီး bug နည်းပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: run မလုပ်ခင် စာရင်းသွင်းတာ
  1. Scope ထဲက declaration အားလုံး စာရင်းသွင်း
  2. function = body အပြည့်၊ var = undefined
  3. let/const/class = TDZ ထဲမှာ စောင့်
  4. Line တစ်ကြောင်းချင်း run ပြီး value ထည့်

Code ကို ရွှေ့တာ မဟုတ်ပါဘူး၊ နာမည်တွေကို ကြိုသိထားတာပါ။

အထွေထွေ ပုံစံ

javascript
useDeclared();                 // OK: declaration is fully hoisted
function useDeclared() { /* ... */ }

console.log(oldVar);           // undefined
var oldVar = value;

console.log(modern);           // ReferenceError: TDZ
let modern = value;

useExpression();               // TypeError (var) or ReferenceError (const)
var useExpression = function () { /* ... */ };

Declaration အမျိုးအစား လေးမျိုးကို line မတိုင်ခင် သုံးရင် ရလဒ် လေးမျိုး ရပါတယ်။ Function declaration ကပဲ အလုပ်လုပ်ပြီး ကျန်တာတွေက undefined ဒါမှမဟုတ် error ပါ။

Example: declaration ငါးမျိုးကို ကြိုသုံးကြည့်တာ

ဒီ example က declaration တစ်မျိုးစီကို ကြေညာတဲ့ line မတိုင်ခင် သုံးကြည့်ပြီး error ဖြစ်ရင် catch ထဲမှာ error ရဲ့ name နဲ့ message အစစ်ကို print ထုတ်ပါတယ်။ ဒါကြောင့် engine က တကယ် ဘာပြောလဲ ကိုယ်တိုင် မြင်ရပါမယ်။

hoisting.js
// 1. var: the binding exists from the start, value is undefined
console.log(x); // undefined (no error)
var x = 5;
console.log(x); // 5

// 2. let/const: the binding exists but is in the temporal dead zone
try {
  console.log(y);
  let y = 10;
} catch (e) {
  console.log(`${e.name}: ${e.message}`);
}

// 3. Function declaration: usable before its line
sayHello();
function sayHello() {
  console.log("Hello from hoisted function!");
}

// 4. Function expression stored in var: the variable is undefined
try {
  sayBye();
  var sayBye = function () {
    console.log("Bye!");
  };
} catch (e) {
  console.log(`${e.name}: ${e.message}`);
}

// 5. Arrow function stored in const: temporal dead zone
try {
  sayLater();
  const sayLater = () => console.log("Later!");
} catch (e) {
  console.log(`${e.name}: ${e.message}`);
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Lines 2-3console.log(x); // undefined (no error)

    var x ကို creation phase မှာ undefined နဲ့ ဖန်တီးထားပြီးပြီမို့ error မတက်ဘဲ undefined ထွက်ပါတယ်။ = 5 ဆိုတဲ့ assignment ကတော့ line 3 ရောက်မှ ဖြစ်ပါတယ်။

  2. Lines 8-9console.log(y);

    y ကို let နဲ့ block ထဲမှာ ကြေညာထားလို့ block စကတည်းက binding ရှိပေမယ့် TDZ ထဲမှာပါ။ ဒါကြောင့် "Cannot access 'y' before initialization" ReferenceError တက်ပါတယ်။ ဒီ message ကို ကိုယ်တိုင် ရေးထားတဲ့ string ကနေ မဟုတ်ဘဲ e.message အစစ်ကနေ ပြထားပါတယ်။

  3. Lines 15-16function sayHello() {

    Function declaration ကို body အပြည့်နဲ့ hoist လုပ်လို့ line 15 က ခေါ်တာ အလုပ်လုပ်ပါတယ်။ Helper function တွေကို ဖိုင်ရဲ့ အောက်ပိုင်းမှာ ထားလို့ ရတာ ဒီအကြောင်းကြောင့်ပါ။

  4. Lines 22-23var sayBye = function () {

    Hoist လုပ်တာက var sayBye ပဲ ဖြစ်ပြီး value က undefined ပါ။ undefined ကို function လို ခေါ်လို့ "sayBye is not a function" TypeError ရပါတယ်။

  5. Lines 32-33const sayLater = () => console.log("Later!");

    Arrow function ကို const ထဲ ထည့်ထားလို့ sayLater က TDZ ထဲမှာ ရှိပါတယ်။ Error က TypeError မဟုတ်ဘဲ ReferenceError ဖြစ်တာကို line 22 နဲ့ နှိုင်းယှဉ်ကြည့်ပါ။

You should see
undefined
5
ReferenceError: Cannot access 'y' before initialization
Hello from hoisted function!
TypeError: sayBye is not a function
ReferenceError: Cannot access 'sayLater' before initialization

TDZ နဲ့ shadowing: အပြင်က variable ကို မမြင်ရတော့တဲ့အခါ

Hoisting ရဲ့ အံ့သြစရာ အကောင်းဆုံး ရလဒ်တစ်ခုက ဒီလိုပါ — function အပြင်မှာ level ရှိနေပေမယ့် function ထဲမှာ let level ကို အောက်ပိုင်းမှာ ကြေညာထားရင် function ထိပ်ပိုင်းက level က အပြင်ကဟာကို မဖတ်တော့ပါဘူး။ Inner level ရဲ့ binding က function စကတည်းက ရှိပြီး outer level ကို ဖုံးထားလို့ TDZ ReferenceError တက်ပါတယ်။ typeof ကလည်း TDZ ထဲမှာ error တက်ပါတယ်၊ လုံးဝ မကြေညာထားတဲ့ နာမည်အတွက်ကျမှ "undefined" ပြန်ပါတယ်။

tdz-shadowing.js
let level = "outer";

function show() {
  try {
    console.log(level);
  } catch (e) {
    console.log(`${e.name}: the inner level is still in the TDZ`);
  }
  let level = "inner";
  console.log(level);
}
show();

console.log(typeof neverDeclared); // "undefined"
try {
  console.log(typeof later);
  let later = 1;
} catch (e) {
  console.log(`typeof inside the TDZ -> ${e.name}`);
}

Function တစ်ခု run တဲ့အခါ ဘာဖြစ်လဲ

Function ကို ခေါ်တိုင်း creation phase နဲ့ execution phase ပြန်ဖြစ်ပါတယ်။ အောက်က diagram က checkout() ကို ခေါ်တဲ့အခါ ဘာတွေ ကြိုဖန်တီးလဲ ပြပါတယ်။ Execution phase မှာ line တစ်ကြောင်းချင်း run ရင်း TDZ ထဲက binding တွေ တစ်ခုချင်း အသုံးပြုလို့ ရလာပါတယ်။

text
Creation phase vs execution phase for checkout()
------------------------------------------------
function checkout() {          CREATION PHASE (before line 1 runs)
  var total = 0;               total    -> undefined
  const tax = 0.05;            tax      -> <uninitialised: TDZ>
  function addTax(n) { ... }   addTax   -> function (full body)
  ...                        EXECUTION PHASE
}                              line 1: total = 0
                               line 2: tax = 0.05  (TDZ ends here)

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

လက်တွေ့ project တွေမှာ hoisting ကို အသုံးချတဲ့ နေရာ တစ်ခုက ဖိုင်တစ်ခုရဲ့ အပေါ်ပိုင်းမှာ အဓိက logic ကို ထားပြီး အသေးစိတ် helper function declaration တွေကို အောက်ပိုင်းမှာ ထားတာပါ။ ဖတ်တဲ့သူက "ဒီ ဖိုင်က ဘာလုပ်လဲ" ကို အရင် မြင်ရပြီး အသေးစိတ်ကို နောက်မှ ကြည့်လို့ရပါတယ်။ ဒါက function declaration တွေ အပြည့် hoist ဖြစ်လို့သာ ဖြစ်နိုင်တာပါ။

ကျန်တဲ့ နေရာတွေမှာတော့ team အများစုက hoisting ကို အားမကိုးဘဲ ကာကွယ်ကြပါတယ်။ ESLint ရဲ့ no-var နဲ့ no-use-before-define rule တွေက var သုံးတာနဲ့ ကြေညာခင် သုံးတာကို သတိပေးပါတယ်။ Code အဟောင်းတွေကို var ကနေ let/const ပြောင်းတဲ့အခါ TDZ ReferenceError အသစ်တွေ ပေါ်လာတတ်ပြီး အဲဒါတွေက အရင်က undefined နဲ့ တိတ်တဆိတ် ဖြစ်နေခဲ့တဲ့ bug အစစ်တွေ ဖြစ်တတ်ပါတယ်။ Interview မေးခွန်းတွေမှာလည်း hoisting output ခန့်မှန်းခိုင်းတာ အရမ်း အဖြစ်များလို့ case တစ်ခုချင်းစီကို အသံထွက်ပြီး ရှင်းပြနိုင်အောင် လေ့ကျင့်ထားတာ တန်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Hoisting က value ကိုပါ ရွှေ့တယ်လို့ ထင်တာ

var x = 5; ကို hoist လုပ်ရင် x = 5 ဆိုတဲ့ assignment က နေရာမှာပဲ ကျန်ပါတယ်။ ကြိုဖတ်ရင် 5 မဟုတ်ဘဲ undefined ရပါတယ်။ Declaration ပဲ ကြိုမှတ်ပုံတင်တယ်၊ initializer (= value) ကတော့ line ရောက်မှ run တယ်လို့ မှတ်ပါ။

2. Function expression ကို declaration လို ကြိုခေါ်တာ

function helper() {} ကို const helper = () => {} လို့ refactor လုပ်လိုက်ရင် ဖိုင်အပေါ်ပိုင်းက helper() ခေါ်တဲ့နေရာတွေ ReferenceError ဖြစ်ကုန်ပါတယ်။ Arrow function ကို ပြောင်းသုံးရင် အသုံးပြုတဲ့ နေရာ မတိုင်ခင် ကြေညာထားပါ။

3. Block ထဲက let က အပြင် variable ကို ဖုံးထားတာ မသိတာ

Function ရဲ့ အောက်ပိုင်းမှာ outer variable နဲ့ နာမည်တူ let တစ်ခု ထပ်ကြေညာလိုက်ရင် အပေါ်ပိုင်းက အဲဒီ နာမည်ကို သုံးတဲ့ code အားလုံး TDZ error ဖြစ်ပါတယ်။ နာမည်တူ variable တွေကို nested scope တွေမှာ ပြန်မသုံးပါနဲ့။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • var ကို မသုံးပါနဲ့၊ ပုံမှန်အားဖြင့် const ကို သုံးပြီး ပြန် assign လုပ်ဖို့ လိုမှ let ကို သုံးပါ — TDZ က အချိန်မတိုင်ခင် သုံးမိတာကို ချက်ချင်း ဖမ်းပေးပါတယ်။
  • Variable တွေကို ပထမဆုံး သုံးမယ့် နေရာနဲ့ နီးနီးမှာ၊ scope ရဲ့ အပေါ်ပိုင်းမှာ ကြေညာပါ။
  • Hoisting ကို တမင် အသုံးချချင်ရင် function declaration တွေကိုပဲ အားကိုးပြီး ESLint ရဲ့ no-use-before-define ကို ဖွင့်ထားပါ။

Mini challenge

Mini challenge: hoisting probe

Function တစ်ခုကို run ပြီး ရလဒ် ဒါမှမဟုတ် error name ကို print ထုတ်တဲ့ probe() helper ရေးပါ။ အဲဒါကို သုံးပြီး declaration ငါးမျိုးကို line မတိုင်ခင် သုံးရင် ဘာဖြစ်လဲ စမ်းပါ။ Run မလုပ်ခင် ရလဒ်ကို ကြိုခန့်မှန်းပြီး ချရေးထားပါ။

  • probe(label, fn) က fn() ကို try/catch ထဲမှာ run ပြီး "label: result" ဒါမှမဟုတ် "label: ErrorName" ကို print ထုတ်ရမယ်
  • var, let, function declaration, var function expression, class ငါးမျိုးကို probe လုပ်ရမယ်
  • Probe တစ်ခုစီရဲ့ function body ထဲမှာ declaration ကို သုံးတဲ့ line ရဲ့ အောက်မှာ ထားရမယ်
hoisting-probe.js
function probe(label, fn) {
  // TODO: run fn() inside try/catch
  // TODO: print "label: result" on success, "label: ErrorName" on failure
}

// TODO: probe("var", ...)                     read a var above its declaration
// TODO: probe("let", ...)                     read a let above its declaration
// TODO: probe("function declaration", ...)    call it above its definition
// TODO: probe("var function expression", ...) call it above the assignment
// TODO: probe("class", ...)                   use new above the class

ရလဒ်: ခန့်မှန်းထားတာနဲ့ ယှဉ်ကြည့်ပါ: var: undefined let: ReferenceError function declaration: 5 var function expression: TypeError class: ReferenceError

အရိပ်အမြွက် ကြည့်ရန်

Arrow function body တစ်ခုစီက scope အသစ်ပါ၊ ဥပမာ probe("var", () => { const r = v; var v = 1; return r; }); မှာ v က arrow function ရဲ့ scope ထဲကို hoist ဖြစ်ပါတယ်။ Error name ကို e.name နဲ့ ယူပါ။

အဖြေ ကြည့်ရန်
hoisting-probe.js
function probe(label, fn) {
  try {
    console.log(`${label}: ${fn()}`);
  } catch (e) {
    console.log(`${label}: ${e.name}`);
  }
}

probe("var", () => {
  const read = v;
  var v = 1;
  return read;
});

probe("let", () => {
  const read = l;
  let l = 1;
  return read;
});

probe("function declaration", () => {
  return add(2, 3);
  function add(a, b) {
    return a + b;
  }
});

probe("var function expression", () => {
  const result = multiply(2, 3);
  var multiply = function (a, b) {
    return a * b;
  };
  return result;
});

probe("class", () => {
  const point = new Point();
  class Point {}
  return point;
});

Quick Quiz 1 / 3

console.log(total); var total = 10; က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

ဘယ် greet ကို ကြေညာတဲ့ line ရဲ့ အပေါ်မှာ greet() လို့ ခေါ်ရင် အလုပ်လုပ်မလဲ။

Quick Quiz 3 / 3

let level = "outer"; function show() { console.log(level); let level = "inner"; } show(); ရဲ့ ရလဒ်က ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Hoisting ဆိုတာ code ကို ရွှေ့တာ မဟုတ်ဘဲ creation phase မှာ declaration တွေကို ကြိုမှတ်ပုံတင်တာပါ။
  • Function declaration က body အပြည့်နဲ့ hoist ဖြစ်ပြီး var က undefined နဲ့ hoist ဖြစ်ပါတယ်။
  • let, const နဲ့ class က declaration line ထိ TDZ ထဲမှာ ရှိပြီး ကြိုသုံးရင် ReferenceError တက်ပါတယ်။
  • Function expression နဲ့ arrow function တွေက variable အလိုက် ပြုမူလို့ ကြိုခေါ်ရင် TypeError ဒါမှမဟုတ် ReferenceError ရပါတယ်၊ သုံးခင် ကြေညာပါ။

MDN — JavaScript Guide — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Hoisting | Thuta Learning