Thuta Learning
JavaScript
IntermediateProgrammingအခြေခံ

The 'this' Keyword

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

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

  • Call site ကို ကြည့်ပြီး this ရဲ့ value ကို ခန့်မှန်းနိုင်ဖို့
  • Method ကို callback အဖြစ် ပို့တဲ့အခါ this ဘာကြောင့် ပျောက်လဲ ရှင်းပြနိုင်ဖို့
  • call, apply, bind နဲ့ this ကို တိတိကျကျ သတ်မှတ်နိုင်ဖို့
  • Arrow function က ဘေးပတ်ဝန်းကျင်ရဲ့ this ကို ဘယ်လို ယူသုံးလဲ၊ ဘယ်နေရာမှာ မသုံးသင့်လဲ ခွဲခြားနိုင်ဖို့

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

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

"ငါ" ဆိုတဲ့ စကားလုံးကို စဉ်းစားကြည့်ပါ။ Aung Kyaw က "ငါ ဗိုက်ဆာတယ်" လို့ ပြောရင် ငါ က Aung Kyaw ဖြစ်ပြီး Ma Ma က အဲဒီ စာကြောင်းအတိုင်း ပြောရင် ငါ က Ma Ma ဖြစ်ပါတယ်။ စာကြောင်းက တူပေမယ့် ဘယ်သူ ပြောလဲဆိုတာက အဓိပ္ပာယ်ကို ဆုံးဖြတ်ပါတယ်။

JavaScript ရဲ့ this လည်း အဲဒီလိုပါပဲ။ Function တစ်ခုထဲက this.name ဆိုတာ "ငါ့ရဲ့ name" လို့ ပြောတာဖြစ်ပြီး "ငါ" ဆိုတာ ဘယ်သူလဲဆိုတာကို function ကို ခေါ်တဲ့ အချိန်မှာ ဆုံးဖြတ်ပါတယ်။ person.greet() လို့ ခေါ်ရင် ငါ က person ဖြစ်ပြီး object မပါဘဲ greet() လို့ပဲ ခေါ်ရင်တော့ ငါ ဆိုတာ မရှိတော့ပါဘူး။

Problem → Why → Solution

ပြဿနာ: callback အဖြစ် ပို့လိုက်တော့ method က ကိုယ့် object ကို မေ့သွားတယ်

counter.increment() ကို တိုက်ရိုက် ခေါ်ရင် ကောင်းကောင်း အလုပ်လုပ်ပါတယ်။ ဒါပေမယ့် တစ်စက္ကန့်ကြာမှ run စေချင်လို့ setTimeout(counter.increment, 1000) လို့ ပို့လိုက်တဲ့အခါ 2 မထွက်ဘဲ NaN ထွက်လာပြီး counter.count ကလည်း 1 မှာပဲ ရပ်နေပါတယ်။ Error တစ်ခုမှ မတက်ပါဘူး။ Function က အတူတူပဲ ဖြစ်ပေမယ့် ခေါ်ပုံ ပြောင်းသွားလို့ပါ။

counter.js
const counter = {
  count: 0,
  increment() {
    this.count++;
    console.log(this.count);
  },
};

counter.increment();                 // 1
setTimeout(counter.increment, 1000); // NaN, and counter.count is still 1

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

counter.increment လို့ ရေးလိုက်တာက function ကို object ကနေ ခွဲထုတ်ပြီး ပို့လိုက်တာပါ၊ counter ကိုပါ ပို့တာ မဟုတ်ပါဘူး။ setTimeout က အချိန်ရောက်တော့ အဲဒီ function ကို ကိုယ့်နည်းကိုယ့်ဟန်နဲ့ ခေါ်ပါတယ် — browser က this အဖြစ် window ကို ပေးပြီး Node.js က သူ့ရဲ့ internal Timeout object ကို ပေးပါတယ်။ ဘယ်လိုပဲ ဖြစ်ဖြစ် this က counter မဟုတ်လို့ this.count က undefined ဖြစ်ပြီး ++ လုပ်တော့ NaN ရပါတယ်။ ပြင်နည်းက bind(counter) ဒါမှမဟုတ် () => counter.increment() ပါ။

Core concept: call site က this ကို ဆုံးဖြတ်တယ်

Function ကို ဘယ်မှာ ရေးထားလဲ မဟုတ်ဘဲ ဘယ်လို ခေါ်လဲ ဆိုတာကို call site လို့ ခေါ်ပါတယ်။ this ရဲ့ value ကို ရှာတဲ့အခါ function body ကို မကြည့်ဘဲ ခေါ်တဲ့ စာကြောင်းကို ကြည့်ပါ။ Regular function (function keyword နဲ့ ရေးတာ၊ method shorthand) တွေအတွက် စည်းမျဉ်း လေးခု ရှိပါတယ်။

ခေါ်ပုံthis က ဘာဖြစ်လဲ
new Fn()အသစ် ဖန်တီးလိုက်တဲ့ object
fn.call(obj) / fn.apply(obj) / fn.bind(obj)()ကိုယ်တိုင် ပေးလိုက်တဲ့ obj
obj.method()အစက် (.) ရှေ့က obj
fn() — ရှေ့မှာ ဘာမှ မပါStrict mode: undefined၊ sloppy script: global object

Rule တွေ တစ်ခုထက်ပိုပြီး ကိုက်နေရင် table ရဲ့ အပေါ်ကဟာက အနိုင်ရပါတယ် — ဥပမာ bind လုပ်ထားတဲ့ function ကို obj.method() ပုံစံနဲ့ ခေါ်ရင်တောင် bind ပေးထားတဲ့ object ပဲ this ဖြစ်ပါတယ်။ Class body နဲ့ ES module တွေက strict mode ဖြစ်လို့ object မပါတဲ့ plain call မှာ this က undefined ဖြစ်ပြီး this.something ကို ဖတ်ရင် TypeError တက်ပါတယ်။ ဒါက error ကို ချက်ချင်း မြင်ရလို့ တကယ်တော့ ကောင်းပါတယ်။

Arrow function တွေက ဒီ စည်းမျဉ်းတွေ အားလုံးနဲ့ မသက်ဆိုင်ပါဘူး။ Arrow function မှာ ကိုယ်ပိုင် this မရှိဘဲ သူ့ကို ရေးထားတဲ့ ဘေးပတ်ဝန်းကျင် (lexical scope) ရဲ့ this ကို ယူသုံးပါတယ်။ call/bind နဲ့လည်း ပြောင်းလို့ မရပါဘူး။ ဒါကြောင့် method ထဲက setTimeout ဒါမှမဟုတ် forEach callback အတွက် arrow function က ပြီးပြည့်စုံပေမယ့် object ရဲ့ method ကိုယ်တိုင်ကို arrow နဲ့ ရေးရင် this က object ကို ညွှန်မှာ မဟုတ်ပါဘူး။

Function အပြင်ဘက် top level မှာ this ကို ဖတ်ရင်လည်း environment အလိုက် ကွာပါတယ်။ Browser ရဲ့ classic script မှာ window၊ ES module မှာ undefined၊ Node.js ရဲ့ CommonJS file မှာတော့ module.exports ဖြစ်ပါတယ်။ ဒီ ကွဲပြားမှုကြောင့် top-level this ကို မှီခိုတဲ့ code မရေးပါနဲ့။ Global object ကို တကယ် လိုရင် environment တိုင်းမှာ အလုပ်လုပ်တဲ့ globalThis ကို သုံးပါ။

မှတ်ထားရမယ့် ပုံစံ
  1. Arrow လား? အပြင်က this ကို သုံး
  2. new လား? object အသစ်
  3. call/apply/bind လား? ပေးတဲ့ object
  4. obj.fn() လား? အစက်ရှေ့က obj
  5. ဘာမှ မပါရင် undefined (strict)

Function body ကို မကြည့်ပါနဲ့ — ခေါ်တဲ့ စာကြောင်းမှာ အစက်ရဲ့ ဘယ်ဘက်ကို ကြည့်ပါ။

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

javascript
obj.method();                   // this = obj
const fn = obj.method; fn();    // this = undefined (strict)
fn.call(other, arg1, arg2);     // this = other, args listed
fn.apply(other, [arg1, arg2]);  // this = other, args in an array
const bound = fn.bind(other);   // new function, this fixed to other
new Constructor();              // this = the new object
const arrow = () => this;       // this = the surrounding this

call နဲ့ apply က function ကို ချက်ချင်း ခေါ်ပြီး argument ပေးပုံသာ ကွာပါတယ်။ bind ကတော့ ချက်ချင်း မခေါ်ဘဲ this ကို အသေချိတ်ထားတဲ့ function အသစ်ကို ပြန်ပေးပါတယ်။

Example: this ရဲ့ ပုံစံ ငါးမျိုး

ဒီ example က this ရဲ့ value ရပုံ ငါးမျိုးကို တစ်ခါစီ ခေါ်ပြထားပါတယ်။ "use strict" ထည့်ထားလို့ browser မှာရော Node မှာပါ ရလဒ်တူပါတယ်: detached call က error တက်ပြီး try...catch က အဲဒါကို ဖမ်းပါတယ်။ setTimeout callback က sync code အားလုံး ပြီးမှ run လို့ "Arrow: 42" က နောက်ဆုံးမှ print ထွက်ပါတယ်၊ ဒါကြောင့် run မလုပ်ခင် output အစဉ်ကို ခန့်မှန်းကြည့်ပါ။

this-keyword.js
"use strict";

// 1. Method call: this is the object before the dot
const person = {
  name: "Aung Kyaw",
  greet() {
    return `Hello from ${this.name}`;
  },
};
console.log(person.greet());

// 2. Detached call: nothing before the dot, so this is undefined
const greet = person.greet;
try {
  greet();
} catch (error) {
  console.log(`Detached: ${error.constructor.name}`);
}

// 3. bind returns a copy with this fixed
const boundGreet = person.greet.bind(person);
console.log(boundGreet());

// 4. Explicit binding with call
function introduce(greeting) {
  return `${greeting}, I am ${this.name}`;
}
const user = { name: "Ma Ma" };
console.log(introduce.call(user, "Hi"));

// 5. An arrow callback keeps the method's this
const timer = {
  value: 42,
  start() {
    setTimeout(() => {
      console.log(`Arrow: ${this.value}`);
    }, 10);
  },
};
timer.start();

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

  1. Line 10console.log(person.greet());

    အစက်ရှေ့မှာ person ရှိလို့ greet ထဲက this က person ဖြစ်ပြီး "Hello from Aung Kyaw" ရပါတယ်။

  2. Lines 13-15const greet = person.greet;

    Function ကို variable ထဲ ကူးလိုက်တာနဲ့ person နဲ့ ဆက်သွယ်မှု ပြတ်သွားပါတယ်။ greet() ကို object မပါဘဲ ခေါ်တော့ strict mode မှာ this က undefined ဖြစ်ပြီး this.name က TypeError ပစ်ပါတယ်။

  3. Line 21const boundGreet = person.greet.bind(person);

    bind က this ကို person နဲ့ အသေချိတ်ထားတဲ့ function အသစ်ကို ပြန်ပေးလို့ boundGreet() ကို ဘယ်လိုပဲ ခေါ်ခေါ် person ကို မြင်ပါတယ်။

  4. Line 29console.log(introduce.call(user, "Hi"));

    call ရဲ့ ပထမ argument (user) က this ဖြစ်ပြီး ကျန်တဲ့ "Hi" က greeting parameter ထဲ ရောက်ပါတယ်။ introduce က ဘယ် object နဲ့မှ မချိတ်ထားပေမယ့် call နဲ့ ငှားသုံးလို့ရပါတယ်။

  5. Lines 35-37setTimeout(() => {

    Arrow function မှာ ကိုယ်ပိုင် this မရှိလို့ start() ရဲ့ this (timer) ကို ယူသုံးပါတယ်။ ဒီနေရာမှာ function () {...} ကို သုံးခဲ့ရင် setTimeout က ခေါ်တဲ့ပုံစံကြောင့် this က timer မဖြစ်တော့ပါဘူး။

You should see
Hello from Aung Kyaw
Detached: TypeError
Hello from Aung Kyaw
Hi, I am Ma Ma
Arrow: 42

call, apply နဲ့ bind နှိုင်းယှဉ်ချက်

Methodချက်ချင်း ခေါ်လားArgument ပေးပုံ
fn.call(obj, a, b)ခေါ်တယ်တစ်ခုချင်း comma နဲ့
fn.apply(obj, [a, b])ခေါ်တယ်Array တစ်ခုထဲမှာ
fn.bind(obj, a)မခေါ်ဘူး၊ function အသစ် ပြန်ပေးကြိုထည့်ထားလို့ရ (partial)

Spread syntax (fn.call(obj, ...args)) ရှိလာပြီးနောက် apply ကို သုံးရတာ နည်းသွားပါပြီ။ bind လုပ်ထားတဲ့ function ကို ထပ် bind လုပ်ရင် ပထမ bind ကပဲ အနိုင်ရပြီး this ကို ပြန်ပြောင်းလို့ မရပါဘူး။ Browser မှာ addEventListener ကို function () {...} နဲ့ ပေးရင် this က event ချိတ်ထားတဲ့ element ဖြစ်ပေမယ့် arrow function နဲ့ ပေးရင် အပြင်က this ကို ယူသုံးလို့ element မဖြစ်ပါဘူး — အဲဒီအခါ event.currentTarget ကို သုံးပါ။

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

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

this ပျောက်တဲ့ bug ကို အများဆုံး တွေ့ရတာ event handler နဲ့ timer တွေမှာပါ။ button.addEventListener("click", this.save) လို့ class ထဲကနေ ရေးလိုက်ရင် save() run တဲ့အချိန်မှာ this က class instance မဟုတ်ဘဲ button element ဖြစ်နေပြီး this.data က undefined ဖြစ်ပါတယ်။ ပြင်နည်း သုံးမျိုး ရှိပါတယ် — constructor ထဲမှာ this.save = this.save.bind(this) လုပ်တာ၊ () => this.save() wrapper သုံးတာ၊ ဒါမှမဟုတ် save = () => {...} ဆိုတဲ့ arrow class field နဲ့ ရေးတာပါ။

Arrow class field က instance တစ်ခုစီမှာ function copy တစ်ခုစီ ဖန်တီးလို့ instance ထောင်ချီ ရှိတဲ့ class အတွက် prototype method ထက် memory ပိုကုန်ပါတယ်။ ဒါကြောင့် callback အဖြစ် တကယ် ပို့ရမယ့် method တွေကိုပဲ ဒီလို ရေးပါ။ React ကို အရင်က class component နဲ့ ရေးခဲ့စဉ်က constructor ထဲမှာ handler တွေကို bind လုပ်တာ standard pattern ဖြစ်ခဲ့ပြီး ခေတ်သစ် function component တွေမှာတော့ this ကို လုံးဝ မသုံးတော့ပါဘူး။

အမှားများ

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

1. Object method ကို arrow function နဲ့ ရေးတာ

const shop = { name: "Mya", show: () => this.name }; မှာ arrow က object literal ကို ကြေညာတဲ့ ပတ်ဝန်းကျင် (module top level) ရဲ့ this ကို ယူပါတယ်။ Object literal ရဲ့ {} က scope အသစ် မဖန်တီးလို့ this က shop မဖြစ်ပါဘူး — module ထဲမှာ undefined ဖြစ်လို့ TypeError တက်ပါတယ်။ Method တွေကို show() { return this.name; } လို shorthand နဲ့ ရေးပါ။

2. Method ထဲက nested regular function

printAll() { this.items.forEach(function (item) { console.log(this.prefix + item); }); } မှာ forEach callback က plain function call ဖြစ်လို့ အထဲက this က printAll ရဲ့ this မဟုတ်ပါဘူး။ Strict mode မှာ undefined ဖြစ်ပြီး this.prefix က TypeError ပေးပါတယ်။ Callback ကို arrow function (item) => ... နဲ့ ပြောင်းရေးရင် အပြင်က this ကို ယူသုံးလို့ ပြေလည်ပါတယ်။

3. Destructuring နဲ့ method ကို ထုတ်ယူတာ

const { greet } = person; greet(); က const greet = person.greet နဲ့ အတူတူပါပဲ — object နဲ့ ဆက်သွယ်မှု ပြတ်သွားပါတယ်။ ဖတ်ရတာ သပ်ရပ်လို့ မသိမသာ ဝင်လာတတ်တဲ့ bug ပါ။ this ကို သုံးတဲ့ method ဆိုရင် person.greet() လို့ပဲ ခေါ်ပါ ဒါမှမဟုတ် bind လုပ်ပြီးမှ ထုတ်ယူပါ။

Mini challenge

Mini challenge: ပျောက်သွားတဲ့ this ကို နည်းသုံးမျိုးနဲ့ ပြင်ပါ

counter.increment ကို callback အဖြစ် ပို့တဲ့အခါ this ပျောက်တာကို ပြပြီး bind၊ arrow wrapper နဲ့ call တို့နဲ့ ပြင်ပြပါ။

  • increment() က this.count ကို 1 တိုးပြီး "<label>: <count>" ကို return ပြန်ရမယ်
  • counter.increment ကို runLater ဆီ တိုက်ရိုက် ပို့ပြီး error ရဲ့ constructor နာမည်ကို print ထုတ်ရမယ်
  • bind နဲ့ တစ်ခါ၊ arrow wrapper နဲ့ တစ်ခါ ပြင်ပြီး ရလဒ်ကို print ထုတ်ရမယ်
  • call ကို သုံးပြီး { label: "likes", count: 10 } object ပေါ်မှာ increment ကို run ကာ count နှစ်ခုလုံးကို print ထုတ်ရမယ်
lost-this.js
"use strict";

const counter = {
  label: "clicks",
  count: 0,
  increment() {
    // TODO: add 1 to this object's count and return "<label>: <count>"
  },
};

function runLater(callback) {
  return callback();
}

// TODO 1: pass counter.increment directly to runLater inside try/catch
//         and print the caught error's constructor name
// TODO 2: fix it with bind
// TODO 3: fix it with an arrow function wrapper
// TODO 4: use call to run increment on { label: "likes", count: 10 },
//         then print counter.count and the other object's count

ရလဒ်: Console မှာ ငါးကြောင်း ထွက်ရပါမယ်: Lost this: TypeError clicks: 1 clicks: 2 likes: 11 2 11

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

runLater ထဲမှာ callback() လို့ object မပါဘဲ ခေါ်လို့ this ပျောက်ပါတယ်။ counter.increment.bind(counter) ဒါမှမဟုတ် () => counter.increment() ကို ပို့ရင် counter ကို ပြန်ရပါတယ်။ call က method ကို တခြား object ပေါ်မှာ ငှားသုံးခွင့်ပေးပါတယ်။

အဖြေ ကြည့်ရန်
lost-this.js
"use strict";

const counter = {
  label: "clicks",
  count: 0,
  increment() {
    this.count += 1;
    return `${this.label}: ${this.count}`;
  },
};

function runLater(callback) {
  return callback(); // no object in front of the call
}

// 1. Passing the method alone loses this
try {
  runLater(counter.increment);
} catch (error) {
  console.log(`Lost this: ${error.constructor.name}`);
}

// 2. Fix with bind
console.log(runLater(counter.increment.bind(counter)));

// 3. Fix with an arrow wrapper
console.log(runLater(() => counter.increment()));

// 4. Borrow the method for another object with call
const likes = { label: "likes", count: 10 };
console.log(counter.increment.call(likes));
console.log(counter.count, likes.count);

Quick Quiz 1 / 3

Example ရဲ့ person object နဲ့ strict mode ထဲမှာ const { greet } = person; greet(); ကို run ရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

const box = { value: 42, show: () => this.value }; ဆိုရင် box.show() က 42 ကို ဘာကြောင့် မပြန်ပေးတာလဲ။

Quick Quiz 3 / 3

function introduce() { return "I am " + this.name; } ဆိုရင် introduce.bind({ name: "A" }).bind({ name: "B" })() က ဘာ ပြန်ပေးမလဲ။

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

  • this ကို function ရေးထားတဲ့ နေရာက မဟုတ်ဘဲ ခေါ်တဲ့ပုံစံ (call site) က ဆုံးဖြတ်ပါတယ်။
  • obj.method() မှာ this = obj၊ object မပါတဲ့ plain call မှာ strict mode ဆိုရင် this = undefined ပါ။
  • call/apply က this ပေးပြီး ချက်ချင်း ခေါ်ပြီး bind က this ချိတ်ထားတဲ့ function အသစ် ပြန်ပေးပါတယ်။
  • Arrow function က အပြင်က this ကို ယူသုံးလို့ method ထဲက callback အတွက် ကောင်းပေမယ့် object method ကိုယ်တိုင်အတွက် မသုံးပါနဲ့။

MDN — Using Classes — MDN Web Docs

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

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

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

The 'this' Keyword | Thuta Learning