Thuta Learning
JavaScript
IntermediateProgrammingအခြေခံ

OOP Introduction

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

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

  • State (data) နဲ့ behavior (method) ကို object တစ်ခုထဲ စုထားရတဲ့ အကြောင်းရင်းကို ရှင်းပြနိုင်ဖို့
  • Encapsulation, inheritance, polymorphism, abstraction ကို ဥပမာ တစ်ခုစီနဲ့ ခွဲပြောနိုင်ဖို့
  • Constructor function နဲ့ class နှစ်မျိုးလုံးကနေ new နဲ့ object ဖန်တီးနိုင်ဖို့
  • OOP class သုံးသင့်တဲ့အချိန်နဲ့ plain object/function က ပိုရိုးရှင်းတဲ့အချိန်ကို ခွဲခြားနိုင်ဖို့

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

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

ဘဏ်စာအုပ်တစ်အုပ်ကို စဉ်းစားကြည့်ပါ။ စာအုပ်ထဲမှာ လက်ကျန်ငွေ (data) ရှိပြီး ငွေသွင်းတာ၊ ငွေထုတ်တာကိုတော့ ဘဏ်ကောင်တာက လုပ်ငန်းစဉ် (behavior) ကနေပဲ လုပ်လို့ရပါတယ်။ ကိုယ်တိုင် ခဲတံနဲ့ ဂဏန်းကို ပြင်ရေးလို့ မရပါဘူး။ OOP ဆိုတာ program ထဲမှာလည်း ဒီလိုပဲ data နဲ့ အဲဒီ data ကို ပြောင်းလဲခွင့်ရှိတဲ့ function တွေကို object တစ်ခုထဲ စုထားတဲ့ ရေးနည်းပါ။

Object တွေ အများကြီး လိုရင် တစ်ခုချင်း လက်နဲ့ မရေးဘဲ ပုံစံခွက် (blueprint) တစ်ခုကနေ ထုတ်ပါတယ်။ JavaScript မှာ အဲဒီ blueprint က constructor function ဒါမှမဟုတ် class ဖြစ်ပြီး new keyword နဲ့ object အသစ် (instance) တစ်ခုစီ ဖန်တီးပါတယ်။

Problem → Why → Solution

ပြဿနာ: data နဲ့ function တွေ ကွဲနေရင် state မှားသွားတတ်

အောက်က shopping cart မှာ items array နဲ့ total ကို object တစ်ခုထဲ ထားပေမယ့် သူတို့ကို ပြောင်းတဲ့ addItem() function က အပြင်မှာ သီးသန့် ရှိနေပါတယ်။ Code ရဲ့ တခြားနေရာက cart.items.push() ကို တိုက်ရိုက် ခေါ်လိုက်တော့ total ကို ဘယ်သူမှ update မလုပ်ဘဲ ကျန်ခဲ့ပါတယ်။ Error တစ်ခုမှ မတက်ဘဲ customer ဆီ ဈေးနှုန်း မှားပြီး ပေါ်သွားပါတယ်။

cart.js
// Data and the function that changes it live apart
const cart = { items: [], total: 0 };

function addItem(cart, name, price) {
  cart.items.push({ name, price });
  cart.total += price;
}

addItem(cart, "Pen", 1.5);
cart.items.push({ name: "Book", price: 12 }); // skips addItem
console.log(cart.items.length, cart.total);    // 2 1.5  <- total is wrong

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

items နဲ့ total ဟာ အမြဲ တစ်ပြိုင်နက် ပြောင်းရမယ့် စည်းမျဉ်း (invariant) တစ်ခု ရှိပေမယ့် အဲဒီ စည်းမျဉ်းကို စောင့်ရှောက်တဲ့ နေရာ တစ်နေရာတည်း မရှိပါဘူး။ Data ကို ဘယ်သူမဆို ဘယ်နေရာကမဆို ပြင်လို့ရနေတာပါ။ OOP က data နဲ့ စည်းမျဉ်းကို object တစ်ခုထဲ ပေါင်းထားပြီး method တွေကနေပဲ ပြောင်းခိုင်းလို့ ဒီ bug မျိုးကို ကာကွယ်ပေးပါတယ်။

Core concept: state + behavior = object

OOP မှာ program ကို object တွေ အချင်းချင်း ဆက်သွယ်နေတဲ့ စနစ်တစ်ခုလို မြင်ပါတယ်။ Object တစ်ခုမှာ state (property တွေမှာ သိမ်းထားတဲ့ data) နဲ့ behavior (အဲဒီ data ကို သုံးတဲ့ method တွေ) ပါပါတယ်။ User, Order, Cart လို domain ထဲက အရာတစ်ခုစီကို object တစ်ခုနဲ့ ကိုယ်စားပြုလိုက်တော့ code ကို ဖတ်ရတာ လက်တွေ့ကမ္ဘာနဲ့ နီးစပ်လာပါတယ်။

Principleဆိုလိုရင်းCart ဥပမာ
EncapsulationData နဲ့ method ကို စုထားပြီး method ကနေပဲ ပြောင်းခိုင်းတာadd() က ဈေးနှုန်းကို စစ်ပြီးမှ item ထည့်တယ်
InheritanceChild class က parent ရဲ့ property နဲ့ method တွေကို ယူသုံးတာGiftCart က Cart ရဲ့ method တွေကို ယူသုံးတယ်
PolymorphismMethod နာမည် တူပေမယ့် object အလိုက် ကွဲပြားစွာ လုပ်တာcart.total() နဲ့ giftCart.total() က ကိုယ့်နည်းနဲ့ကိုယ် တွက်တယ်
Abstractionရှုပ်ထွေးတဲ့ အသေးစိတ်ကို ဖုံးပြီး ရိုးရှင်းတဲ့ interface ပဲ ပြတာCaller က summary() ကိုပဲ ခေါ်ပြီး ဘယ်လို တွက်လဲ မသိရဘူး

JavaScript က Java လို class-based language မဟုတ်ဘဲ prototype-based language ပါ။ Object တစ်ခုက တခြား object (သူ့ရဲ့ prototype) ဆီကနေ method တွေကို ချိတ်ဆက်ပြီး ယူသုံးပါတယ်။ ES2015 (ES6) မှာ ပါလာတဲ့ class syntax ဟာ အဲဒီ prototype စနစ်အပေါ်မှာ ဖတ်ရလွယ်အောင် တင်ထားတဲ့ ရေးနည်းပါ — class တစ်ခုကို typeof နဲ့ စစ်ရင် "function" ပဲ ထွက်ပါတယ်။ ဒီ prototype အကြောင်းကို နောက် lesson တွေမှာ အသေးစိတ် ကြည့်ပါမယ်။

OOP ကို သုံးတယ်ဆိုတာ class hierarchy အဆင့်ဆင့် နက်နက် ဆောက်ရမယ်လို့ မဆိုလိုပါဘူး။ Object တစ်ခုက တခြား object သေးသေးလေးတွေကို ပိုင်ဆိုင်ပြီး အလုပ်ခွဲပေးတဲ့ composition က inheritance ထက် ပိုရိုးရှင်းတာ များပါတယ်။ Data မရှိဘဲ input ကနေ output တွက်ရုံ function ဆိုရင်တော့ plain function တစ်ခုက class တစ်ခုထက် ပိုရှင်းပါတယ်။

Instance တစ်ခုစီမှာ ကိုယ်ပိုင် state ရှိတာကိုလည်း မှတ်ထားပါ။ new Cart("Su Su") နဲ့ new Cart("Ko Ko") ကို ဖန်တီးရင် blueprint တူပေမယ့် items array နှစ်ခုက သီးခြားစီပါ။ Cart တစ်ခုထဲ ပစ္စည်းထည့်တာက နောက်တစ်ခုကို မထိခိုက်ပါဘူး၊ ဒါက global variable တွေနဲ့ ရေးတဲ့အခါ မရနိုင်တဲ့ အားသာချက်ပါ။

မှတ်ထားရမယ့် ပုံစံ
  1. Blueprint (class) တစ်ခု ရေး
  2. new က instance အသစ် ထုတ်
  3. Instance တစ်ခုစီမှာ ကိုယ်ပိုင် state
  4. State ကို method ကနေပဲ ပြောင်း

Blueprint က တစ်ခုတည်း၊ object တွေက အများကြီး — ဒါပေမယ့် object တစ်ခုစီရဲ့ data က သီးခြားစီပါ။

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

javascript
class Thing {
  constructor(value) {
    this.value = value;        // state
  }
  doSomething() {              // behavior
    return this.value;
  }
}

const instance = new Thing(initialValue);
instance.doSomething();

constructor က object အသစ်ရဲ့ state ကို စတင်သတ်မှတ်ပြီး class ထဲက method တွေက instance တိုင်း သုံးနိုင်တဲ့ behavior ပါ။ new Thing(...) က instance တစ်ခု ပြန်ပေးပြီး instance.method() နဲ့ ခေါ်ပါတယ်။

Example: constructor function နဲ့ class နှိုင်းယှဉ်ချက်

ဒီ example မှာ object ဖန်တီးနည်း နှစ်မျိုးကို ဘေးချင်းယှဉ်ထားပါတယ်။ အပေါ်ပိုင်းက class syntax မပေါ်ခင်ကတည်းက သုံးခဲ့တဲ့ constructor function ဖြစ်ပြီး အောက်ပိုင်းက ခေတ်သစ် class ပါ။ နှစ်ခုလုံးမှာ new keyword နဲ့ this ကို သုံးတာကို သတိထားကြည့်ပါ။

oop-intro.js
// Constructor function (the older way)
function Person(name, age) {
  this.name = name;
  this.age = age;
  this.greet = function () {
    return `Hello, I'm ${this.name}`;
  };
}

const person1 = new Person("Aung Kyaw", 25);
console.log(person1.greet());
console.log(`Age: ${person1.age}`);

// ES6 class (the modern way)
class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return `${this.name} makes a sound`;
  }
}

const dog = new Animal("Buddy");
console.log(dog.speak());

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

  1. Line 2function Person(name, age) {

    စာလုံးအကြီးနဲ့ စတဲ့ နာမည်က "new နဲ့ ခေါ်ရမယ့် constructor" ဆိုတဲ့ convention ပါ။ ကိုယ်တိုင်က ရိုးရိုး function တစ်ခုပါပဲ။

  2. Line 5this.greet = function () {

    greet ကို constructor ထဲမှာ ဖန်တီးလို့ new ခေါ်တိုင်း function အသစ်တစ်ခု ထပ်ဖန်တီးပါတယ်။ Instance တစ်ထောင်ဆို greet copy တစ်ထောင် ရှိပါမယ် — class method တွေက ဒီပြဿနာ မရှိပါဘူး။

  3. Line 10const person1 = new Person("Aung Kyaw", 25);

    new က object အလွတ်တစ်ခု ဖန်တီးပြီး Person ထဲမှာ this ကို အဲဒီ object ဖြစ်စေပါတယ်။ Constructor ပြီးသွားတော့ အဲဒီ object ကို person1 ထဲ ပြန်ပေးပါတယ်။

  4. Lines 16-18constructor(name) {

    Class ရဲ့ constructor က constructor function ရဲ့ body နဲ့ အလုပ်တူပါတယ် — instance ရဲ့ state ကို စတင်သတ်မှတ်ပေးပါတယ်။

  5. Lines 19-21speak() {

    Class body ထဲက method တွေကို instance တိုင်းက မျှဝေသုံးပါတယ်၊ copy ကူးမထားပါဘူး။ dog.speak() ကို ခေါ်တော့ this က dog ဖြစ်လို့ "Buddy makes a sound" ရပါတယ်။

You should see
Hello, I'm Aung Kyaw
Age: 25
Buddy makes a sound

Polymorphism ကို မြင်သာအောင်

Polymorphism ဆိုတာ caller က object အမျိုးအစား မသိဘဲ method နာမည် တစ်ခုတည်းကို ခေါ်ပြီး object တစ်ခုစီက ကိုယ့်နည်းနဲ့ကိုယ် တုံ့ပြန်တာပါ။ အောက်က loop က shape ဘယ်အမျိုးအစားလဲ စစ်စရာမလိုဘဲ area() ကိုပဲ ခေါ်ပါတယ်။ Shape အမျိုးအစား အသစ် ထပ်ထည့်ရင်လည်း loop ကို ပြင်စရာ မလိုပါဘူး။

shapes.js
class Square {
  constructor(side) { this.side = side; }
  area() { return this.side * this.side; }
}

class Circle {
  constructor(radius) { this.radius = radius; }
  area() { return Math.PI * this.radius ** 2; }
}

for (const shape of [new Square(3), new Circle(1)]) {
  console.log(shape.constructor.name, shape.area().toFixed(2));
}

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

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

E-commerce app တစ်ခုမှာ Cart object က items နဲ့ discount code ကို ပိုင်ဆိုင်ပြီး add(), remove(), total() method တွေကနေပဲ ပြောင်းခွင့်ပေးပါတယ်။ ဒါကြောင့် "ဈေးနှုန်း အနုတ် မဖြစ်ရ"၊ "discount က total ထက် မများရ" ဆိုတဲ့ စည်းမျဉ်းတွေကို တစ်နေရာတည်းမှာ ရေးပြီး test လုပ်လို့ရပါတယ်။ UI component ကလည်း cart.total() ကိုပဲ ခေါ်ပြီး ဘယ်လို တွက်လဲ သိစရာ မလိုပါဘူး။

Browser ထဲမှာလည်း OOP က နေရာတိုင်းမှာ ရှိပါတယ်။ new Date(), new Map(), new URL("https://...") တို့ဟာ built-in class တွေကနေ instance ဖန်တီးတာဖြစ်ပြီး date.getFullYear() လို method တွေက abstraction ပါ — date ကို အတွင်းမှာ ဘယ်လို သိမ်းထားလဲ ကိုယ်မသိဘဲ သုံးနေတာပါ။ Node.js ရဲ့ EventEmitter၊ game တစ်ခုထဲက Player နဲ့ Enemy လို object တွေကလည်း state နဲ့ behavior ကို စုထားတဲ့ ဥပမာ ကောင်းတွေပါ။

အမှားများ

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

1. Constructor ကို new မပါဘဲ ခေါ်တာ

const p = Person("Aung Kyaw", 25); လို့ ရေးမိရင် object အသစ် မဖန်တီးပါဘူး။ Strict mode မှာ this က undefined ဖြစ်လို့ this.name = name မှာ TypeError တက်ပြီး sloppy mode မှာတော့ global object ပေါ်ကို property တွေ ချိတ်သွားပြီး p က undefined ဖြစ်ပါတယ်။ Class ကို new မပါဘဲ ခေါ်ရင်တော့ JavaScript က TypeError ကို ချက်ချင်း ပစ်ပေးလို့ ဒီအမှားကို ကာကွယ်ပြီးသား ဖြစ်ပါတယ်။

2. Method ကို ကျော်ပြီး property ကို တိုက်ရိုက် ပြင်တာ

cart.items.push(...) ဒါမှမဟုတ် account.balance = 1000000 လို တိုက်ရိုက် ပြင်လိုက်ရင် method ထဲက စစ်ဆေးမှုတွေ အလုပ်မလုပ်တော့ပါဘူး။ ပြင်ခွင့်ရှိတဲ့ data ဆိုရင် method တစ်ခု ထုတ်ပေးပြီး လုံးဝ မပြင်စေချင်တဲ့ data ကိုတော့ နောက် lesson မှာ လေ့လာမယ့် private field (#) နဲ့ ကာကွယ်ပါ။

3. အရာတိုင်းကို class လုပ်ပစ်တာ

State မရှိတဲ့ formatPrice(amount) လို helper ကို class PriceFormatter { format() {...} } အဖြစ် ပြောင်းလိုက်ရင် new ခေါ်ရတာ၊ this ကို ဂရုစိုက်ရတာပဲ ပိုလာပါတယ်။ ကိုယ်ပိုင် data မရှိတဲ့ logic ဆိုရင် plain function ကိုပဲ သုံးပါ။

Best Practices

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

  • Code အသစ်မှာ constructor function အစား class syntax ကို သုံးပါ — new မေ့ရင် error တက်ပြီး method တွေကလည်း အလိုအလျောက် မျှဝေထားပါတယ်။
  • Constructor ထဲမှာ object ကို မှန်ကန်တဲ့ state နဲ့ စတင်ပါ၊ တစ်ဝက်တစ်ပျက် ဖန်တီးထားတဲ့ object ကို ပြန်မပေးပါနဲ့။
  • Class နာမည်ကို နာမ်နဲ့ (Cart, Invoice)၊ method နာမည်ကို ကြိယာနဲ့ (add, calculateTotal) ပေးပါ၊ ဒါမှ cart.add() လို ဖတ်ရတာ စာကြောင်းလို ဖြစ်ပါမယ်။
  • Class တစ်ခုက အလုပ်တစ်ခုတည်းကို တာဝန်ယူပါစေ၊ method နှစ်ဆယ်ကျော်လာရင် class သေးသေးလေးတွေ ခွဲဖို့ အချိန်ရောက်ပါပြီ။

Mini challenge

Mini challenge: စည်းမျဉ်း စောင့်တဲ့ Cart class

Problem section က cart bug ကို ပြင်ဖို့ items နဲ့ total စည်းမျဉ်းကို ကိုယ်တိုင် စောင့်ရှောက်တဲ့ Cart class တစ်ခု ရေးပါ။

  • constructor(owner) က owner ကို သိမ်းပြီး items array အလွတ်နဲ့ စရမယ်
  • add(name, price) က price ဟာ အပေါင်း number မဟုတ်ရင် ငြင်းပြီး ရလဒ် message ကို return ပြန်ရမယ်
  • total() က items အားလုံးရဲ့ price ကို ပေါင်းရမယ် — total ကို သီးသန့် variable မှာ မသိမ်းရ
  • summary() က "<owner> has <n> items, total <ဒသမ နှစ်နေရာ>" ကို return ပြန်ရမယ်
cart-class.js
class Cart {
  constructor(owner) {
    // TODO: store the owner and start with an empty items array
  }

  add(name, price) {
    // TODO: reject a price that is not a positive number
    // TODO: otherwise store { name, price } and return "Added <name>"
  }

  total() {
    // TODO: add up every item's price
  }

  summary() {
    // TODO: "<owner> has <n> items, total <total with 2 decimals>"
  }
}

// TODO: create a cart, add two good items and one bad one, print the summary

ရလဒ်: Console မှာ ဒီလို လေးကြောင်း ထွက်ရပါမယ်: Added Pen Added Book Rejected Ghost: invalid price Su Su has 2 items, total 13.50

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

typeof price !== "number" || price <= 0 နဲ့ မမှန်တဲ့ price ကို စစ်ပါ။ total() မှာ reduce((sum, item) => sum + item.price, 0) ကို သုံးနိုင်ပြီး summary() ထဲကနေ this.total() ကို ခေါ်ပါ။

အဖြေ ကြည့်ရန်
cart-class.js
class Cart {
  constructor(owner) {
    this.owner = owner;
    this.items = [];
  }

  add(name, price) {
    if (typeof price !== "number" || price <= 0) {
      return `Rejected ${name}: invalid price`;
    }
    this.items.push({ name, price });
    return `Added ${name}`;
  }

  total() {
    return this.items.reduce((sum, item) => sum + item.price, 0);
  }

  summary() {
    const count = this.items.length;
    return `${this.owner} has ${count} items, total ${this.total().toFixed(2)}`;
  }
}

const cart = new Cart("Su Su");
console.log(cart.add("Pen", 1.5));
console.log(cart.add("Book", 12));
console.log(cart.add("Ghost", -3));
console.log(cart.summary());

Quick Quiz 1 / 3

Cart ရဲ့ add() က ဈေးနှုန်း အနုတ်ကို ငြင်းပြီး items ကို method ကနေပဲ ပြောင်းခွင့်ပေးတာ ဘယ် OOP principle လဲ။

Quick Quiz 2 / 3

class Animal {} ရေးပြီးနောက် console.log(typeof Animal) က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

Example ထဲက Person constructor နဲ့ person1, person2 နှစ်ခု ဖန်တီးရင် person1.greet === person2.greet က ဘာဖြစ်မလဲ။

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

  • Object တစ်ခုမှာ state (property) နဲ့ behavior (method) ကို အတူတကွ စုထားပါတယ်။
  • Encapsulation, inheritance, polymorphism, abstraction ဆိုတဲ့ principle လေးခုက OOP ရဲ့ အခြေခံပါ။
  • Constructor function ဖြစ်ဖြစ် class ဖြစ်ဖြစ် new keyword က instance အသစ်တစ်ခု ဖန်တီးပေးပါတယ်။
  • JavaScript က prototype-based ဖြစ်ပြီး class က အဲဒီအပေါ်က syntax ပါ၊ deep hierarchy ထက် composition နဲ့ plain function က ပိုရိုးရှင်းတတ်ပါတယ်။

MDN — Using Classes — MDN Web Docs

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

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

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

OOP Introduction | Thuta Learning