ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
အိမ်မှာ ဆားရှာတယ်ဆိုပါစို့။ ကိုယ့်မီးဖိုချောင်မှာ အရင်ရှာပြီး မတွေ့ရင် အိမ်နီးချင်းကို မေးပါတယ်၊ သူ့မှာလည်း မရှိရင် ရပ်ကွက်ဆိုင်ကို သွားပါတယ်။ ဆိုင်မှာလည်း မရှိမှ "ဆား မရှိဘူး" လို့ လက်လျှော့ပါတယ်။
JavaScript object တွေလည်း ဒီလိုပဲ အလုပ်လုပ်ပါတယ်။ Object တိုင်းမှာ prototype လို့ခေါ်တဲ့ "အိမ်နီးချင်း" object တစ်ခု ချိတ်ထားပြီး ကိုယ့်ဆီမှာ မရှိတဲ့ property ကို prototype ဆီ၊ ပြီးတော့ prototype ရဲ့ prototype ဆီ ဆက်တိုက် မေးသွားပါတယ်။ ဒီ ချိတ်ဆက်မှု ကြိုးတန်းကို prototype chain လို့ ခေါ်ပြီး JavaScript ရဲ့ inheritance တစ်ခုလုံး ဒီပေါ်မှာ တည်ဆောက်ထားတာပါ။
Problem → Why → Solution
ပြဿနာ: object ထဲမှာ မထည့်ရသေးတဲ့ key က ရှိနေပြီးသား ဖြစ်နေတယ်
စာသားတစ်ခုထဲက စကားလုံး ရေတွက်တဲ့ code ကို plain object {} နဲ့ ရေးလိုက်ပါတယ်။ "apple" အတွက် မှန်ပေမယ့် user က "constructor" ဆိုတဲ့ စကားလုံး ရိုက်ထည့်လိုက်တော့ count က ဂဏန်းမဟုတ်ဘဲ ထူးဆန်းတဲ့ string တစ်ခု ဖြစ်သွားပါတယ်။ counts ထဲကို constructor ဆိုတဲ့ key ကို ဘယ်တုန်းကမှ မထည့်ခဲ့ပါဘူး။
const counts = {};
for (const word of ["apple", "constructor", "apple"]) {
counts[word] = (counts[word] || 0) + 1;
}
console.log(counts.apple); // 2
console.log(counts.constructor); // "function Object() { [native code] }1"ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
{} က Object.prototype နဲ့ ချိတ်ထားလို့ counts["constructor"] ကို ဖတ်တဲ့အခါ counts ပေါ်မှာ မတွေ့တော့ prototype ဆီ တက်ရှာပြီး Object function ကို တွေ့ပါတယ်။ Function က truthy ဖြစ်လို့ || 0 အလုပ်မလုပ်ဘဲ function + 1 က string ပေါင်းခြင်း ဖြစ်သွားပါတယ်။ Prototype chain ကို နားလည်မှ ဒီ bug ကို ရှင်းနိုင်ပါတယ် — Object.hasOwn နဲ့ စစ်တာ၊ Object.create(null) ဒါမှမဟုတ် Map သုံးတာနဲ့ ပြင်နိုင်ပါတယ်။
Core concept: prototype chain မှာ property ရှာပုံ
Object တိုင်းမှာ [[Prototype]] လို့ ခေါ်တဲ့ hidden link တစ်ခု ရှိပြီး တခြား object တစ်ခု ဒါမှမဟုတ် null ကို ညွှန်ပါတယ်။ obj.key ကို ဖတ်ရင် JavaScript က (1) obj ကိုယ်တိုင်ပေါ်မှာ ရှာ၊ (2) မတွေ့ရင် သူ့ prototype ပေါ်မှာ ရှာ၊ (3) chain အဆုံး null ရောက်တဲ့အထိ ဆက်ရှာပြီး (4) ဘယ်မှာမှ မတွေ့ရင် undefined ပြန်ပေးပါတယ်။ ဒီ link ကို Object.getPrototypeOf(obj) နဲ့ ဖတ်ပါ — __proto__ ဆိုတဲ့ accessor ကတော့ legacy feature ဖြစ်လို့ code အသစ်မှာ ရှောင်ပါ။
Function တိုင်းမှာ prototype ဆိုတဲ့ ရိုးရိုး property တစ်ခု ပါပါတယ်။ new Vehicle("car") က object အသစ်ရဲ့ [[Prototype]] ကို Vehicle.prototype အဖြစ် သတ်မှတ်ပါတယ်။ ဒါကြောင့် Vehicle.prototype.describe ကို တစ်ခါ ရေးလိုက်ရင် Vehicle instance တိုင်းက အဲဒီ function တစ်ခုတည်းကို မျှဝေသုံးပြီး memory ချွေတာပါတယ်။ Class method တွေလည်း ဒီ prototype object ပေါ်မှာပဲ နေပါတယ် — class syntax က ဒီ ပုံစံကို ရေးရလွယ်အောင် လုပ်ထားတာပါ။ Lookup က live ဖြစ်လို့ instance ဖန်တီးပြီးမှ prototype ပေါ် method ထပ်ထည့်ရင်လည်း ရှိပြီးသား object တွေက ချက်ချင်း မြင်ရပါတယ်။
ဖတ်တာနဲ့ ရေးတာ မတူပါဘူး။ ဖတ်တဲ့အခါ chain ကို တက်ရှာပေမယ့် bike.wheels = 2 လို assignment က prototype ကို မပြင်ဘဲ bike ပေါ်မှာ own property အသစ် ဖန်တီးပါတယ်။ အဲဒီ own property က prototype ပေါ်က wheels ကို ဖုံးထားလို့ shadowing လို့ ခေါ်ပါတယ်။ Property တစ်ခု object ကိုယ်တိုင်ပေါ်မှာ ရှိလား စစ်ချင်ရင် Object.hasOwn(obj, key) ကို သုံးပါ — key in obj ကတော့ chain တစ်ခုလုံးကို စစ်လို့ inherited property အတွက်ပါ true ပေးပါတယ်။
ရိုးရိုး object တစ်ခုရဲ့ chain က အရမ်းတိုပါတယ် — obj → Object.prototype → null ပါပဲ။ toString(), hasOwnProperty() လို method တွေကို object literal တိုင်းက သုံးနိုင်တာ Object.prototype ပေါ်မှာ ရှိနေလို့ပါ။ Array တစ်ခုရဲ့ chain ကတော့ arr → Array.prototype → Object.prototype → null ဖြစ်ပါတယ်။ Chain ကို ဘယ်လောက်ပဲ ရှည်ရှည် ဖတ်တိုင်း အောက်ကနေ အပေါ်ကို ရှာသွားလို့ အောက်ဆုံးမှာ ရှိတဲ့ (object နဲ့ အနီးဆုံး) property က အမြဲ အနိုင်ရပါတယ်။
- Object ကိုယ်တိုင်ပေါ်မှာ အရင်ရှာ
- မတွေ့ရင် [[Prototype]] ဆီ တက်
- null ရောက်ရင် undefined
- Assignment က object ပေါ်မှာပဲ ရေး
ဖတ်တာက chain တစ်လျှောက် တက်ရှာပေမယ့် ရေးတာက prototype ကို ဘယ်တော့မှ မပြင်ပါဘူး (setter မရှိရင်)။
အထွေထွေ ပုံစံ
function Constructor(arg) {
this.ownProp = arg; // per instance
}
Constructor.prototype.sharedMethod = function () {
return this.ownProp; // shared by all instances
};
const instance = new Constructor(value);
Object.getPrototypeOf(instance) === Constructor.prototype; // true
Object.hasOwn(instance, "ownProp"); // true
Object.hasOwn(instance, "sharedMethod"); // falseInstance တစ်ခုစီ ကွဲပြားတဲ့ data ကို constructor ထဲမှာ this ပေါ် ထည့်ပြီး မျှဝေသုံးမယ့် method ကို Constructor.prototype ပေါ် ထည့်ပါတယ်။ Object.getPrototypeOf နဲ့ link ကို စစ်ပြီး Object.hasOwn နဲ့ own property ဟုတ်/မဟုတ် စစ်ပါတယ်။
Example: Vehicle prototype
Vehicle constructor က type ကို instance တစ်ခုစီပေါ် ထည့်ပြီး describe() နဲ့ wheels ကို prototype ပေါ်မှာ မျှဝေထားပါတယ်။ bike က wheels ကို shadow လုပ်ပြီး နောက်ဆုံး သုံးကြောင်းက property တွေ ဘယ်မှာ ရှိလဲ ဆိုတာကို Object.hasOwn() နဲ့ စစ်ပါတယ်။ Object.hasOwn() က car.hasOwnProperty() ကို တိုက်ရိုက် ခေါ်တာကို အစားထိုးတဲ့ ခေတ်သစ် နည်းလမ်းပါ။
// Constructor function
function Vehicle(type) {
this.type = type;
}
// Shared by every Vehicle through the prototype
Vehicle.prototype.describe = function () {
return `This is a ${this.type}`;
};
Vehicle.prototype.wheels = 4;
const car = new Vehicle("car");
const bike = new Vehicle("bike");
bike.wheels = 2; // own property on bike shadows the prototype's
console.log(car.describe());
console.log(`Car wheels: ${car.wheels}`);
console.log(bike.describe());
console.log(`Bike wheels: ${bike.wheels}`);
// Own property or inherited?
console.log(Object.hasOwn(car, "type")); // true
console.log(Object.hasOwn(car, "describe")); // false (lives on the prototype)
console.log(Object.getPrototypeOf(car) === Vehicle.prototype); // trueCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 7-9
Vehicle.prototype.describe = function () {describe ကို prototype ပေါ်မှာ တစ်ခါပဲ ဖန်တီးလို့ car နဲ့ bike က function တစ်ခုတည်းကို မျှဝေသုံးပါတယ်။ Arrow function မဟုတ်ဘဲ function expression ဖြစ်ရပါမယ်၊ ဒါမှ this က ခေါ်တဲ့ object ဖြစ်ပါမယ်။
- Line 10
Vehicle.prototype.wheels = 4;Method တွေသာမက ရိုးရိုး value ကိုလည်း prototype ပေါ် ထားလို့ရပါတယ်။ ကိုယ်ပိုင် wheels မရှိတဲ့ instance တိုင်းက 4 ကို default အဖြစ် မြင်ပါတယ်။
- Line 14
bike.wheels = 2; // own property on bike shadows the prototype'sAssignment က Vehicle.prototype.wheels ကို မပြင်ဘဲ bike ပေါ်မှာ own property အသစ် ဖန်တီးပါတယ်။ ဒါကြောင့် car က 4 ကို ဆက်မြင်ပြီး bike ကတော့ 2 ကို မြင်ပါတယ်။
- Line 16
console.log(car.describe());car ပေါ်မှာ describe မရှိလို့ Vehicle.prototype ဆီ တက်ရှာပြီး တွေ့ပါတယ်။ ခေါ်တဲ့အချိန်မှာ this က car ဖြစ်လို့ "This is a car" ရပါတယ်။
- Lines 22-23
console.log(Object.hasOwn(car, "describe")); // false (lives on the prototype)type က constructor ထဲမှာ car ပေါ် တိုက်ရိုက် ထည့်ထားလို့ own property ဖြစ်ပြီး describe ကတော့ inherited ဖြစ်လို့ false ပါ။ car.describe ကို ခေါ်လို့ရနေပေမယ့် car ကိုယ်တိုင်ရဲ့ property မဟုတ်ပါဘူး။
This is a car
Car wheels: 4
This is a bike
Bike wheels: 2
true
false
trueObject.create နဲ့ class ရဲ့ prototype
Object.create(proto) က constructor မလိုဘဲ prototype ကို ကိုယ်တိုင် ရွေးပြီး object ဖန်တီးပေးပါတယ်။ Object.create(null) ဆိုရင် prototype လုံးဝ မရှိတဲ့ "dictionary" object ရလို့ problem section က constructor bug မဖြစ်တော့ပါဘူး။ Class တစ်ခုရဲ့ method တွေ ဘယ်မှာ ရှိလဲ ကြည့်ချင်ရင် Object.getOwnPropertyNames(ClassName.prototype) နဲ့ စစ်နိုင်ပါတယ်။
const animal = {
describe() { return `${this.name} says ${this.sound}`; },
};
const cat = Object.create(animal);
cat.name = "Whiskers";
cat.sound = "meow";
console.log(cat.describe()); // Whiskers says meow
console.log(Object.getPrototypeOf(cat) === animal); // true
const dict = Object.create(null);
console.log(dict.constructor); // undefined
class Point { norm() { return 0; } }
console.log(Object.getOwnPropertyNames(Point.prototype)); // [ 'constructor', 'norm' ]လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Browser DevTools မှာ object တစ်ခုကို ဖွင့်ကြည့်ရင် [[Prototype]] ဆိုတဲ့ အပိုင်းကို တွေ့ရပါမယ်။ "ဒီ method က ဘယ်ကလာတာလဲ"၊ "ဘာကြောင့် ဒီ value ပြောင်းသွားတာလဲ" ဆိုတဲ့ debugging မေးခွန်းတွေကို အဲဒီ chain ကို တစ်ဆင့်ချင်း ဖွင့်ကြည့်ပြီး ဖြေရပါတယ်။ Array method တွေ (map, filter) က Array.prototype ပေါ်မှာ ရှိပြီး string method တွေက String.prototype ပေါ်မှာ ရှိတာလည်း အဲဒီမှာ မြင်ရပါတယ်။
Security ဘက်မှာ prototype pollution က တကယ့် vulnerability အမျိုးအစား တစ်ခုပါ။ User ပို့လိုက်တဲ့ JSON ထဲမှာ "__proto__" ဆိုတဲ့ key ပါပြီး deep merge function က အဲဒီ key ကို မစစ်ဘဲ ကူးရင် Object.prototype ပေါ်ကို isAdmin: true လို property ရောက်သွားပြီး app ထဲက object တိုင်းက အဲဒါကို မြင်ပါတယ်။ လူသုံးများတဲ့ merge utility တချို့မှာတောင် ဒီ bug မျိုး ရှိခဲ့ဖူးပါတယ်။ ကာကွယ်ဖို့ __proto__, constructor, prototype key တွေကို ပယ်တာ၊ user data အတွက် Map ဒါမှမဟုတ် Object.create(null) သုံးတာ လုပ်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Prototype ပေါ်မှာ array ဒါမှမဟုတ် object ကို မျှဝေထားတာ
Vehicle.prototype.passengers = [] လို့ ရေးပြီး car.passengers.push("Su") လုပ်ရင် push က assignment မဟုတ်လို့ own property အသစ် မဖန်တီးဘဲ prototype ပေါ်က array တစ်ခုတည်းကို ပြင်ပါတယ်။ ဒါကြောင့် bike.passengers မှာလည်း "Su" ပေါ်လာပါတယ်။ Instance တစ်ခုစီ ကွဲပြားရမယ့် array/object တွေကို constructor ထဲမှာ this.passengers = [] လို့ ဖန်တီးပါ။
2. Prototype method ကို arrow function နဲ့ ရေးတာ
Vehicle.prototype.describe = () => `This is a ${this.type}` လို့ ရေးရင် arrow function မှာ ကိုယ်ပိုင် this မရှိလို့ car.describe() ခေါ်ပေမယ့် this က car မဖြစ်ပါဘူး။ ES module ထဲမှာ ရေးထားရင် this က undefined ဖြစ်လို့ TypeError တက်ပြီး classic script မှာတော့ "This is a undefined" လို အဖြေမှား ထွက်ပါတယ်။ Prototype method တွေကို function expression ဒါမှမဟုတ် class method နဲ့ပဲ ရေးပါ။
3. Built-in prototype တွေကို ပြင်တာ
Array.prototype.last = function () {...} လို ထည့်တာက ရေးရလွယ်ပေမယ့် library တခြားတစ်ခု ဒါမှမဟုတ် JavaScript ရဲ့ နောက် version က နာမည်တူ method ထည့်ရင် တိုက်မိပါတယ်။ Library အဟောင်းတစ်ခုက Array.prototype.flatten ကို ထည့်ထားခဲ့လို့ standard method ကို flat လို့ နာမည်ပြောင်းခဲ့ရတာ ဥပမာ ကောင်းပါ။ Helper function last(array) ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Code အသစ်မှာ prototype ကို လက်နဲ့ မရေးဘဲ class syntax ကို သုံးပါ၊ prototype အကြောင်း နားလည်ထားတာကိုတော့ debugging အတွက် သုံးပါ။
- Prototype ကို ဖတ်ဖို့ Object.getPrototypeOf၊ own property စစ်ဖို့ Object.hasOwn ကို သုံးပြီး __proto__ ကို ရှောင်ပါ။
- User ထည့်တဲ့ string တွေကို key အဖြစ် သုံးမယ်ဆိုရင် plain {} အစား Map ဒါမှမဟုတ် Object.create(null) ကို ရွေးပါ။
- Prototype ပေါ်မှာ method နဲ့ မပြောင်းလဲတဲ့ default value တွေပဲ ထားပြီး ပြောင်းလဲနိုင်တဲ့ array/object တွေကို constructor ထဲမှာ ဖန်တီးပါ။
Mini challenge
Mini challenge: property ဘယ်မှာ ရှိလဲ ရှာပါ
Prototype chain ကို ကိုယ်တိုင် တက်ရှာပြီး property တစ်ခု ဘယ်အဆင့်မှာ ရှိလဲ ပြောပေးတဲ့ whereIs(obj, key) function ကို ရေးပါ။
- Object ကိုယ်တိုင်ပေါ်မှာ ရှိရင် "<key>: own property" ကို ပြန်ပေးရမယ်
- Prototype ပေါ်မှာ တွေ့ရင် "<key>: found on prototype #<level>" ကို ပြန်ပေးရမယ် (ပထမ prototype က #1)
- Chain အဆုံးထိ မတွေ့ရင် "<key>: not found" ကို ပြန်ပေးရမယ်
- Object.hasOwn နဲ့ Object.getPrototypeOf ကိုပဲ သုံးရမယ်၊ in operator မသုံးရ
function whereIs(obj, key) {
// TODO: start at obj with level 0
// TODO: loop until the current object is null
// - if the current object owns key, report "own property" or the level
// - otherwise move to its prototype and add 1 to the level
// TODO: report "not found"
}
// TODO: a Shape constructor that stores name on the instance
// TODO: a describe() method on Shape.prototype
// TODO: create a square and check "name", "describe", "toString", "area"ရလဒ်: Console မှာ လေးကြောင်း ထွက်ရပါမယ်: name: own property describe: found on prototype #1 toString: found on prototype #2 area: not found
အရိပ်အမြွက် ကြည့်ရန်
while (current !== null) loop ထဲမှာ current = Object.getPrototypeOf(current) နဲ့ တစ်ဆင့်ချင်း တက်ပါ။ toString က Object.prototype ပေါ်မှာ ရှိလို့ square ကနေ နှစ်ဆင့် အပေါ်မှာ တွေ့ရပါမယ်။
အဖြေ ကြည့်ရန်
function whereIs(obj, key) {
let current = obj;
let level = 0;
while (current !== null) {
if (Object.hasOwn(current, key)) {
if (level === 0) return `${key}: own property`;
return `${key}: found on prototype #${level}`;
}
current = Object.getPrototypeOf(current);
level++;
}
return `${key}: not found`;
}
function Shape(name) {
this.name = name;
}
Shape.prototype.describe = function () {
return `A ${this.name}`;
};
const square = new Shape("square");
for (const key of ["name", "describe", "toString", "area"]) {
console.log(whereIs(square, key));
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Object တိုင်းမှာ [[Prototype]] link ရှိပြီး property ဖတ်တဲ့အခါ null ရောက်တဲ့အထိ chain တစ်လျှောက် ရှာပါတယ်။
- new Fn() က instance ကို Fn.prototype နဲ့ ချိတ်လို့ prototype ပေါ်က method တွေကို instance အားလုံး မျှဝေသုံးပါတယ်။
- Assignment က own property အသစ် ဖန်တီးပြီး prototype ပေါ်က value ကို shadow လုပ်ပါတယ်။
- Object.hasOwn နဲ့ Object.getPrototypeOf ကို သုံးပြီး built-in prototype တွေကို မပြင်ပါနဲ့၊ user-keyed data အတွက် Map ကို ရွေးပါ။
MDN — Using Classes — MDN Web Docs