Thuta Learning
JavaScript
IntermediateProgrammingအခြေခံ

JS Objects

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

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

  • Object literal ရေးပြီး property တွေကို ဖတ်၊ ထည့်၊ ပြင်၊ ဖျက်နိုင်ဖို့
  • Key က variable ထဲမှာ ရှိတဲ့အခါ dot အစား bracket notation ကို ရွေးနိုင်ဖို့
  • Object ကို assign လုပ်ရင် reference ကူးတာဖြစ်ပြီး {...obj} က shallow copy ပဲ ဖြစ်ကြောင်း ရှင်းပြနိုင်ဖို့
  • မရှိနိုင်တဲ့ property တွေကို ?. နဲ့ ?? သုံးပြီး crash မဖြစ်အောင် ဖတ်နိုင်ဖို့

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

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

မှတ်ပုံတင်ကတ်တစ်ကတ်ကို စဉ်းစားကြည့်ပါ။ "အမည်: အောင်အောင်"၊ "အသက်: 30"၊ "မြို့: ရန်ကုန်" လို ခေါင်းစဉ်နဲ့ တန်ဖိုး အတွဲတွေ ပါပါတယ်။ အချက်အလက် အားလုံးက လူတစ်ယောက်တည်းနဲ့ ဆိုင်လို့ ကတ်တစ်ကတ်ထဲ စုထားတာပါ။ "အသက် ဘယ်လောက်လဲ" လို့ သိချင်ရင် "အသက်" ဆိုတဲ့ ခေါင်းစဉ်ကို ရှာရုံပါပဲ။

JavaScript object က အဲဒီ ကတ်ပါပဲ။ { firstName: "Aung", age: 30 } လို key: value အတွဲ (property) တွေကို တွန့်ကွင်း ထဲမှာ စုထားပြီး person.age လို့ ဖတ်ပါတယ်။ Variable သုံးခု သီးခြားစီ ထားမယ့်အစား ဆက်စပ်နေတဲ့ data တွေကို နာမည်တစ်ခုတည်းအောက်မှာ အတူတူ သယ်သွားလို့ ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: draft ကို ပြင်ရင် မူရင်း profile ပါ ပြောင်းသွားတယ်

Profile ပြင်တဲ့ form တစ်ခုမှာ user က Cancel နှိပ်ရင် မူရင်းအတိုင်း ပြန်ပြနိုင်အောင် draft copy တစ်ခုကို ပြင်ခိုင်းထားပါတယ်။ Name ကို ပြင်ပြီး Cancel နှိပ်တော့ မူရင်း name မှန်နေပါတယ်။ ဒါပေမယ့် city ကို ပြင်ပြီး Cancel နှိပ်တော့ မူရင်း profile ရဲ့ city ပါ ပြောင်းသွားပါတယ်။

profile-draft.js
const user = { name: "Su", address: { city: "Yangon" } };

const draft = { ...user };       // looks like a full copy
draft.name = "Su Su";            // fine: top-level value was copied
draft.address.city = "Mandalay"; // nested object is shared!

console.log(user.name);          // Su
console.log(user.address.city);  // Mandalay (the original changed)

ဘာကြောင့် city ပါ ပြောင်းသွားတာလဲ

Object variable တွေက object ကိုယ်တိုင်ကို မသိမ်းဘဲ object ရှိတဲ့ နေရာ (reference) ကို သိမ်းပါတယ်။ {...user} က အပေါ်ဆုံး level ရှိ property တွေကို object အသစ်ထဲ ကူးပေမယ့် address ရဲ့ value က reference ဖြစ်လို့ reference ကိုပဲ ကူးပါတယ်။ ဒါကြောင့် draft.address နဲ့ user.address က address object တစ်ခုတည်းကို ညွှန်ပြနေပြီး တစ်ဖက်က ပြင်ရင် ကျန်တစ်ဖက်ကပါ မြင်ရပါတယ်။ ဒါကို shallow copy လို့ ခေါ်ပါတယ်။

Core concept: property, reference နဲ့ ဘေးကင်းတဲ့ access

Object literal { key: value, ... } ထဲမှာ key တွေက string ဖြစ်ပြီး value က ဘာမဆို ဖြစ်နိုင်ပါတယ် — number, string, array, နောက်ထပ် object, function တောင် ရပါတယ်။ Property ကို ဖတ်ဖို့ နည်းနှစ်နည်း ရှိပါတယ်။ person.age လို dot notation က key ကို code ထဲမှာ တိုက်ရိုက် သိတဲ့အခါ သုံးပြီး person["age"] ဒါမှမဟုတ် person[key] လို bracket notation က key က variable ထဲမှာ ရှိတဲ့အခါ ဒါမှမဟုတ် "first-name" လို space, dash ပါတဲ့ key တွေအတွက် သုံးပါတယ်။

မရှိတဲ့ property ကို ဖတ်ရင် error မတက်ဘဲ undefined ရပါတယ်။ ဒါပေမယ့် undefined ရဲ့ property ကို ထပ်ဖတ်ရင် (user.address.city မှာ address မရှိရင်) TypeError တက်ပါတယ်။ user.address?.city လို optional chaining (?.) က ဘယ်ဘက်က null ဒါမှမဟုတ် undefined ဖြစ်ရင် crash မဖြစ်ဘဲ undefined ပြန်ပေးပြီး ?? (nullish coalescing) က အဲဒီ undefined ကို default value နဲ့ အစားထိုးပေးပါတယ်။ person.city = "Yangon" လို assign လုပ်ရင် property အသစ် ထည့်တာ ဒါမှမဟုတ် ပြင်တာ ဖြစ်ပြီး delete person.city က ဖျက်ပါတယ်။

Value အဖြစ် function ပါတဲ့ property ကို method လို့ ခေါ်ပါတယ်။ person.fullName() လို ခေါ်တဲ့အခါ method ထဲက this က dot ရဲ့ ဘယ်ဘက်က object (person) ဖြစ်ပါတယ်၊ this ရဲ့ အသေးစိတ်ကို နောက် lesson မှာ လေ့လာပါမယ်။

အရေးအကြီးဆုံး အချက်က object တွေကို reference နဲ့ ကိုင်တွယ်တာပါ။ const b = a; က object အသစ် မဆောက်ဘဲ နာမည်နောက်တစ်ခု ပေးရုံပါ၊ b ကနေ ပြင်ရင် a ကနေလည်း မြင်ရပါတယ်။ === ကလည်း content ကို မနှိုင်းဘဲ object တစ်ခုတည်း ဟုတ်မဟုတ် စစ်တာမို့ { x: 1 } === { x: 1 } က false ပါ။ const နဲ့ ကြေညာထားတာက variable ကို object တခြားတစ်ခုဆီ ပြန်ညွှန်ခွင့် မပေးရုံပဲ ဖြစ်ပြီး object ထဲက property တွေကိုတော့ ပြင်လို့ ရနေဆဲပါ။ Nested object တွေပါ ကူးချင်ရင် structuredClone() ကို သုံးပါ။

Object ထဲက property အားလုံးကို ဖြတ်သွားချင်ရင် Object.keys(obj) က key array ကို၊ Object.values(obj) က value array ကို၊ Object.entries(obj) က [key, value] အတွဲ array ကို ပြန်ပေးပါတယ်။ အဲဒီ array တွေကို for...of နဲ့ ဖြတ်လို့ ရပါတယ်။ Property တစ်ခု ရှိမရှိ စစ်ချင်ရင် "phone" in user ဒါမှမဟုတ် Object.hasOwn(user, "phone") ကို သုံးပါ၊ value က undefined ဖြစ်နေတာနဲ့ key လုံးဝ မရှိတာကို ခွဲခြားပေးလို့ပါ။

မှတ်ထားရမယ့် ပုံစံ: variable က လိပ်စာကို ကိုင်ထားတယ်
  1. { } က object အသစ်တစ်ခု ဆောက်
  2. Variable က အဲဒီ object ရဲ့ reference ကို သိမ်း
  3. b = a က reference ကိုပဲ ကူး
  4. a ဒါမှမဟုတ် b ကနေ ပြင်ရင် object တစ်ခုတည်း ပြောင်း

Object အသစ် လိုချင်ရင် { } ဒါမှမဟုတ် copy function တစ်ခုခုနဲ့ ကိုယ်တိုင် ဆောက်ရပါတယ်။

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

javascript
const objectName = {
  key: value,
  methodName() {
    return this.key;
  },
};

objectName.key;           // dot notation
objectName[variableKey];  // bracket notation
objectName.newKey = value; // add or update
objectName.maybe?.deeper ?? fallback; // safe read

Property တွေကို comma နဲ့ ခွဲရေးပြီး methodName() { } က method ရေးနည်း အတိုပါ။ အောက်က line တွေက ဖတ်တာ၊ variable key နဲ့ ဖတ်တာ၊ ထည့်/ပြင်တာ နဲ့ ဘေးကင်းကင်း ဖတ်တာ ပုံစံတွေပါ။

Example: person object နဲ့ method

person object မှာ data property သုံးခုနဲ့ method တစ်ခု ပါပါတယ်။ Property ကို dot နဲ့ ဖတ်တာ၊ method ခေါ်တာ၊ variable key နဲ့ ဖတ်တာ၊ property အသစ် ထည့်တာ အားလုံးကို တစ်နေရာတည်းမှာ မြင်ရပါမယ်။

person.js
const person = {
  firstName: "Aung",
  lastName: "Hla",
  age: 30,
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.firstName);
console.log(person.fullName());

const key = "age";
console.log(person[key]);
person.city = "Yangon";
console.log(Object.keys(person));

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

  1. Lines 5-7fullName: function() {

    Value က function ဖြစ်လို့ fullName က method ပါ။ person.fullName() လို့ ခေါ်တဲ့အခါ this က person ဖြစ်ပြီး this.firstName က "Aung" ပါ။

  2. Line 10console.log(person.firstName);

    Key ကို code ထဲမှာ တိုက်ရိုက် သိလို့ dot notation ကို သုံးပါတယ်။ "Aung" ကို print ထုတ်ပါတယ်။

  3. Line 11console.log(person.fullName());

    ကွင်း () ပါလို့ method ကို run ပြီး "Aung Hla" ကို ပြန်ရပါတယ်။ ကွင်းမပါရင် function ကိုယ်တိုင်ကို print ထုတ်မှာပါ။

  4. Lines 13-14console.log(person[key]);

    key variable ထဲမှာ "age" ရှိလို့ bracket notation က person.age ကို ဖတ်ပြီး 30 ရပါတယ်။ person.key လို့ ရေးရင် "key" ဆိုတဲ့ နာမည်ရှိ property ကို ရှာပြီး undefined ရပါမယ်။

  5. Lines 15-16person.city = "Yangon";

    person ကို const နဲ့ ကြေညာထားပေမယ့် property အသစ် ထည့်လို့ ရပါတယ်။ Object.keys() က key တွေကို ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း array အဖြစ် ပြန်ပေးပါတယ်။

You should see
Aung
Aung Hla
30
[ 'firstName', 'lastName', 'age', 'fullName', 'city' ]

Reference, copy နဲ့ ဘေးကင်းတဲ့ access ကို run ကြည့်ခြင်း

text
const b = a copies the reference, not the object
------------------------------------------------
  variable          heap
  +-------+
  |   a   |----+
  +-------+    |     +----------------+
               +---->|  { score: 99 } |
  +-------+    |     +----------------+
  |   b   |----+
  +-------+
  b.score = 99 changes the one object both names point to

အောက်က snippet မှာ reference မျှသုံးပုံ၊ === က identity ကို စစ်ပုံ၊ structuredClone() နဲ့ nested object ပါ ကူးပုံ၊ ?. နဲ့ ?? နဲ့ မရှိတဲ့ property ကို ဘေးကင်းကင်း ဖတ်ပုံကို run ကြည့်လို့ ရပါတယ်။

references.js
const a = { score: 1 };
const b = a;
b.score = 99;
console.log(a.score);                // 99: same object
console.log({ x: 1 } === { x: 1 });  // false: two different objects

const user = { name: "Su", address: { city: "Yangon" } };
const deep = structuredClone(user);
deep.address.city = "Mandalay";
console.log(user.address.city);      // Yangon: nested object was copied too

const settings = { theme: "dark" };
console.log(settings.fontSize ?? 14);       // 14
console.log(user.phone?.number ?? "none");  // none, no TypeError

Object တစ်ခုကို loop ပတ်ခြင်း

Settings page တစ်ခုမှာ setting အားလုံးကို "name: value" ပုံစံနဲ့ ပြချင်တယ်ဆိုပါစို့။ Object.entries() က property တစ်ခုစီကို [key, value] array အဖြစ် ပေးပြီး for...of က အဲဒါတွေကို တစ်ခုချင်း ဖြတ်ပါတယ်။ Key တွေကို object ထဲ ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း ရပါတယ် (integer နဲ့ တူတဲ့ key တွေကတော့ ငယ်စဉ်ကြီးလိုက် အရင်ရောက်ပါတယ်)။

settings.js
const settings = { theme: "dark", fontSize: 16, notifications: true };

for (const entry of Object.entries(settings)) {
  console.log(entry[0] + ": " + entry[1]);
}

console.log("Has theme?", "theme" in settings);
console.log("Has language?", Object.hasOwn(settings, "language"));

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

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

API တစ်ခုကနေ ရလာတဲ့ JSON response တိုင်းက JavaScript ထဲ ရောက်တာနဲ့ object ဖြစ်သွားပါတယ်၊ ဒါကြောင့် user profile, product, order စတာတွေ အားလုံး object တွေပါ။ Profile မှာ email လို မဖြစ်မနေ လိုတဲ့ field တွေနဲ့ phone, address လို ရှိချင်မှ ရှိမယ့် field တွေ ရောနေတတ်ပါတယ်။ ရှိချင်မှ ရှိတဲ့ field တွေကို profile.phone ?? "not provided" လို ဖတ်တာက page တစ်ခုလုံး crash မဖြစ်အောင် ကာကွယ်ပါတယ်။

React, Redux လို library တွေက state ကို တိုက်ရိုက် မပြင်ဘဲ object အသစ် ဆောက်ပြီး အစားထိုးဖို့ (immutable update) တောင်းပါတယ်၊ old object နဲ့ new object ကို === နဲ့ နှိုင်းပြီး ပြောင်းလဲမှု ရှိမရှိ ဆုံးဖြတ်လို့ပါ။ ဒါကြောင့် profile ကို update လုပ်တဲ့ function တွေက spread copy နဲ့ object အသစ် ပြန်ပေးပြီး nested address ကိုလည်း သီးခြား ကူးရပါတယ်။ Test ထဲမှာ update ပြီးတဲ့နောက် မူရင်း object မပြောင်းကြောင်း စစ်တာက problem section ထဲက bug မျိုးကို ဖမ်းပေးပါတယ်။

အမှားများ

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

1. Variable key ကို dot နဲ့ ဖတ်တာ

const field = "email"; user.field ဆိုရင် JavaScript က "field" ဆိုတဲ့ နာမည်ရှိ property ကို ရှာပြီး undefined ပြန်ပါတယ်။ Key က variable ထဲမှာ ရှိရင် user[field] လို bracket notation ကို အမြဲ သုံးပါ။

2. Spread copy ကို deep copy လို့ ထင်တာ

{...user} က အပေါ်ဆုံး level ကိုပဲ ကူးလို့ nested object တွေကို ပြင်ရင် မူရင်းပါ ပြောင်းပါတယ်။ Nested level ကိုပါ ကိုယ်တိုင် spread လုပ်တာ ({ ...user, address: { ...user.address } }) ဒါမှမဟုတ် structuredClone(user) ကို သုံးပါ။

3. Object နှစ်ခုကို === နဲ့ နှိုင်းတာ

Content တူတဲ့ object နှစ်ခုကို === နဲ့ နှိုင်းရင် false ရပါတယ်၊ === က object တစ်ခုတည်း ဟုတ်မဟုတ်ကိုပဲ စစ်လို့ပါ။ Content ကို နှိုင်းချင်ရင် လိုတဲ့ property တွေကို တစ်ခုချင်း နှိုင်းပါ။

Best Practices

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

  • Function တွေကို ဝင်လာတဲ့ object ကို တိုက်ရိုက် မပြင်ဘဲ object အသစ် ပြန်ပေးအောင် ရေးပါ၊ bug ရှာရ ပိုလွယ်ပါတယ်။
  • Property နာမည်တွေကို firstName လို camelCase နဲ့ တစ်ပုံစံတည်း ပေးပါ၊ ဒါမှ dot notation နဲ့ ဖတ်လို့ ရပါတယ်။
  • API ကနေ လာတဲ့ ရှိချင်မှ ရှိတဲ့ field တိုင်းကို ?. နဲ့ ?? သုံးပြီး ဖတ်ပြီး default value ကို တစ်နေရာတည်းမှာ သတ်မှတ်ပါ။

Mini challenge

Mini challenge: profile ကို မူရင်း မထိဘဲ update လုပ်ခြင်း

Profile object နဲ့ ပြောင်းလဲချက် object ကို လက်ခံပြီး object အသစ် ပြန်ပေးတဲ့ updateProfile() နဲ့ ရှိချင်မှ ရှိတဲ့ phone ကို ဘေးကင်းကင်း ဖတ်တဲ့ describeProfile() ကို ရေးပါ။

  • updateProfile က မူရင်း profile ကိုရော nested address ကိုပါ မပြောင်းရ
  • changes ထဲမှာ address ပါရင် ပါတဲ့ field ကိုပဲ အစားထိုးပြီး ကျန်တဲ့ address field တွေ (zip) ကို ထိန်းထားရမယ်
  • describeProfile က phone မရှိရင် "not provided" ကို ?? နဲ့ ပြရမယ်
  • Update ပြီးနောက် မူရင်းနဲ့ အသစ်ကို print ထုတ်ပြီး object တစ်ခုတည်း မဟုတ်ကြောင်း စစ်ရမယ်
profile.js
function updateProfile(profile, changes) {
  // TODO: copy the top level and apply changes
  // TODO: copy address separately and merge changes.address into it
  // TODO: return the new object
}

function describeProfile(profile) {
  // TODO: "Name (City), phone: ..." with "not provided" as the fallback
}

const original = {
  name: "Aye Aye",
  email: "aye@example.com",
  address: { city: "Yangon", zip: "11181" },
};

// TODO: update email and city, then print new values, old values and identity checks

ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: aye.aye@example.com Mandalay 11181 aye@example.com Yangon Same object? false Same address? false Aye Aye (Mandalay), phone: not provided

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

{ ...profile, ...changes } နဲ့ အပေါ်ဆုံး level ကို ပေါင်းပြီး address ကိုတော့ { ...profile.address, ...changes.address } နဲ့ သီးခြား ပြန်ဆောက်ပါ။ changes.address က undefined ဖြစ်နေရင်လည်း ...undefined က ဘာမှ မထည့်ပါဘူး။

အဖြေ ကြည့်ရန်
profile.js
function updateProfile(profile, changes) {
  const updated = { ...profile, ...changes };
  updated.address = { ...profile.address, ...changes.address };
  return updated;
}

function describeProfile(profile) {
  const phone = profile.phone ?? "not provided";
  return profile.name + " (" + profile.address.city + "), phone: " + phone;
}

const original = {
  name: "Aye Aye",
  email: "aye@example.com",
  address: { city: "Yangon", zip: "11181" },
};

const updated = updateProfile(original, {
  email: "aye.aye@example.com",
  address: { city: "Mandalay" },
});

console.log(updated.email, updated.address.city, updated.address.zip);
console.log(original.email, original.address.city);
console.log("Same object?", updated === original);
console.log("Same address?", updated.address === original.address);
console.log(describeProfile(updated));

Quick Quiz 1 / 3

const key = "age"; const p = { age: 30, key: "oops" }; console.log(p.key, p[key]); က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

const a = { n: 1 }; const b = a; b.n = 2; console.log(a.n); က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

const user = { name: "Su" }; ဆိုရင် ဘယ် expression က TypeError တက်မလဲ။

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

  • Object က key: value property တွေကို နာမည်တစ်ခုအောက် စုထားပြီး key သိရင် dot, variable ထဲမှာ ရှိရင် bracket နဲ့ ဖတ်ပါတယ်။
  • မရှိတဲ့ property က undefined ပေးပြီး ?. နဲ့ ?? က nested data ကို crash မဖြစ်အောင် ဖတ်စေပါတယ်။
  • Object variable တွေက reference ကို ကိုင်ထားလို့ b = a က object အသစ် မဆောက်ပါဘူး၊ === ကလည်း identity ကို စစ်ပါတယ်။
  • {...obj} က shallow copy ပဲ ဖြစ်လို့ nested object တွေကို သီးခြား ကူးပါ ဒါမှမဟုတ် structuredClone() ကို သုံးပါ။

MDN — JavaScript Guide — MDN Web Docs

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

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

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

JS Objects | Thuta Learning