ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဖဲထုပ်တစ်ထုပ်ကို စဉ်းစားကြည့်ပါ။ Spread ဆိုတာ ဖဲထုပ်ကို စားပွဲပေါ်မှာ တစ်ချပ်ချင်း ဖြန့်ချလိုက်တာပါ။ [...cards, newCard] လို့ ရေးရင် ဖဲအဟောင်းတွေကို ဖြန့်ပြီး အသစ်တစ်ချပ်ထည့်ကာ ဖဲထုပ်အသစ်တစ်ထုပ် ဖြစ်လာပါတယ်။ မူရင်းဖဲထုပ်ကတော့ မပြောင်းပါဘူး။
Rest က ပြောင်းပြန်ပါ။ စားပွဲပေါ်မှာ ပြန့်ကျဲနေတဲ့ ဖဲတွေထဲက ပထမတစ်ချပ်ကို ကိုယ်ယူပြီး ကျန်တာအားလုံးကို ထုပ်တစ်ထုပ်အဖြစ် ပြန်စုလိုက်တာပါ။ Symbol က ... တစ်ခုတည်းပါ။ ဖြန့်တာလား စုတာလားဆိုတာ ရေးထားတဲ့နေရာက ဆုံးဖြတ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: copy ကူးတယ်ထင်ပြီး မူရင်းကို ပြင်မိတာ
App တစ်ခုမှာ user အသစ်တိုင်းအတွက် default setting တွေကို စပေးထားပါတယ်။ Developer က const settings = defaults လို့ ရေးပြီး user တစ်ယောက်အတွက် theme ကို "dark" လို့ ပြောင်းလိုက်ပါတယ်။ ဒါပေမဲ့ = က object ကို copy မကူးပါဘူး၊ object တစ်ခုတည်းကို ညွှန်းတဲ့ နာမည်နောက်တစ်ခု ပေးလိုက်ရုံပါ။
ရလဒ်က နောက်မှဝင်လာတဲ့ user တိုင်း dark theme နဲ့ စတာပါ။ Bug report က "တစ်ခါတစ်လေ setting တွေ ကိုယ့်ဘာသာ ပြောင်းနေတယ်" လို့ပဲ ရောက်လာလို့ ရှာရခက်ပါတယ်။ Argument အရေအတွက် မသေချာတဲ့ function တွေမှာလည်း arguments ဆိုတဲ့ array မဟုတ်တဲ့ object ကို သုံးရလို့ reduce လို method တွေ တိုက်ရိုက် မရပါဘူး။
const defaults = { theme: "light", fontSize: 14 };
function settingsFor(user) {
const settings = defaults; // not a copy: same object
if (user.likesDark) settings.theme = "dark";
return settings;
}
settingsFor({ likesDark: true });
console.log(defaults.theme); // "dark" - every later user is affected
function oldSum() {
return arguments.reduce((a, b) => a + b); // TypeError: not a function
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
JavaScript မှာ object နဲ့ array variable တွေက value ကိုယ်တိုင်ကို မကိုင်ဘဲ reference (memory ထဲက နေရာ) ကို ကိုင်ပါတယ်။ = က reference ကိုပဲ copy ယူလို့ variable နှစ်ခုက object တစ်ခုတည်းကို ပြင်နေကြတာပါ။ { ...defaults } က property တွေကို object အသစ်ထဲ ဖြန့်ထည့်လို့ ပြဿနာ ပြေလည်ပါတယ်။ arguments ကတော့ array-like object သာဖြစ်လို့ ...numbers ဆိုတဲ့ rest parameter က array အစစ်ကို ပေးပါတယ်။
Core concept: ဖြန့်တာလား စုတာလား
... ကို တွေ့တိုင်း "ဒီနေရာမှာ JavaScript က value တွေကို လိုချင်နေတာလား၊ variable နာမည်တွေကို လိုချင်နေတာလား" လို့ မေးကြည့်ပါ။ Value တွေ လိုတဲ့နေရာ (array literal၊ object literal၊ function call ရဲ့ argument) မှာဆိုရင် spread ဖြစ်ပြီး ဖြန့်ချပေးပါတယ်။ နာမည်တွေ လိုတဲ့နေရာ (parameter list၊ destructuring pattern) မှာဆိုရင် rest ဖြစ်ပြီး စုပေးပါတယ်။
| နေရာ | အလုပ် | ဥပမာ |
|---|---|---|
| Array literal | Spread: element တွေ ဖြန့် | [...a, ...b] |
| Function call | Spread: argument တွေ ဖြန့် | Math.max(...nums) |
| Object literal | Spread: property တွေ ကူး | { ...obj, id: 2 } |
| Parameter list | Rest: argument တွေ စု | function f(...args) |
| Destructuring | Rest: ကျန်တာ စု | const [x, ...more] = arr |
Array spread က iterable မှန်သမျှ (array၊ string၊ Set၊ Map) နဲ့ အလုပ်လုပ်ပါတယ်။ ဒါကြောင့် [...new Set(list)] က duplicate ဖယ်ထားတဲ့ array ကို ပေးပြီး [..."hi"] က ["h", "i"] ကို ပေးပါတယ်။ ရိုးရိုး object ကတော့ iterable မဟုတ်လို့ [...{ a: 1 }] က TypeError တက်ပါတယ်။ Object spread ကတော့ object ကိုယ်တိုင်ရဲ့ enumerable property တွေကို copy ကူးပါတယ်။
Object spread မှာ property နာမည်တူရင် နောက်မှ ရေးတဲ့ value က နိုင်ပါတယ်။ ဒါကြောင့် default ကို အရင်၊ override ကို နောက်မှ ရေးရပါတယ်။ Rest မှာတော့ ...rest က အမြဲ နောက်ဆုံးမှာ ရှိရပြီး တစ်ခုပဲ ရှိခွင့်ရှိပါတယ်။ နောက်ဆုံးမဟုတ်ရင် SyntaxError တက်ပါတယ်။
အရေးအကြီးဆုံးကတော့ spread ဟာ shallow copy ဖြစ်တာပါ။ အပေါ်ဆုံးအဆင့်ကိုပဲ အသစ်ဆောက်ပြီး အထဲက object နဲ့ array တွေကိုတော့ reference အတိုင်း မျှဝေထားပါတယ်။ Nested data ကို update လုပ်ချင်ရင် အဆင့်တိုင်းကို spread လုပ်ရပါမယ်၊ ဒါမှမဟုတ် deep copy လိုရင် structuredClone() ကို သုံးနိုင်ပါတယ်။
null နဲ့ undefined ကို spread လုပ်ရင်
Object spread က null နဲ့ undefined ကို ဘာမှမရှိသလို ကျော်သွားပါတယ်။ { ...null } က {} ဖြစ်လို့ { ...defaults, ...options } ကို options မပါဘဲ ခေါ်လည်း ဘေးကင်းပါတယ်။ Array spread ကတော့ မတူပါဘူး။ [...null] နဲ့ [...undefined] က iterable မဟုတ်လို့ TypeError တက်ပါတယ်။ Array မရှိနိုင်တဲ့နေရာမှာ [...(list ?? [])] လို့ ကာကွယ်ပါ။
- ... ဘယ်နေရာမှာ ရှိလဲ ကြည့်
- Value နေရာဆို spread (ဖြန့်)
- နာမည်နေရာဆို rest (စု)
- Copy က အပေါ်ဆုံးအဆင့်ပဲ အသစ်
Object ပေါင်းတဲ့အခါ ဘယ်ကနေ ညာကို ဖတ်ပါ။ ညာဘက်ဆုံးက property က နောက်ဆုံး ရေးတာမို့ နိုင်ပါတယ်။
အခြေခံ ပုံစံ
// Spread: expand values
const newArray = [...arrayA, extraItem, ...arrayB];
const newObject = { ...baseObject, keyToOverride: newValue };
someFunction(...argsArray);
// Rest: gather values
function fn(firstParam, ...restParams) { /* restParams is an array */ }
const [head, ...tail] = someArray;
const { unwanted, ...kept } = someObject;Literal နဲ့ function call ထဲမှာဆိုရင် spread ဖြစ်ပြီး ဖြန့်ပေးပါတယ်။ Parameter နဲ့ destructuring pattern ထဲမှာဆိုရင် rest ဖြစ်ပြီး ကျန်တာကို စုပေးပါတယ်။ Rest က အမြဲ နောက်ဆုံးမှာ ရှိရပါတယ်။
Spread နဲ့ rest ကို နေရာခြောက်မျိုးမှာ run ကြည့်မယ်
အောက်က code မှာ spread သုံးနေရာ၊ rest နှစ်နေရာနဲ့ shallow copy ရဲ့ အန္တရာယ်ကို ပြတဲ့ အပိုင်းတစ်ခု ပါပါတယ်။ နောက်ဆုံး line မှာ copy ကို ပြင်ခဲ့ပေမယ့် original.tags ကို print ထုတ်ထားတာ သတိထားပါ။ ဘာထွက်မလဲ ကြိုတွေးကြည့်ပါ။
// Spread in an array literal: copy and extend
const base = [1, 2, 3];
const extended = [...base, 4, 5];
console.log(extended);
// Spread in a function call: one array becomes separate arguments
console.log(Math.max(...base));
// Spread in an object literal: the property written last wins
const defaults = { theme: "light", fontSize: 14 };
const userPrefs = { fontSize: 18 };
const settings = { ...defaults, ...userPrefs };
console.log(settings);
// Rest parameter: gather any number of arguments into an array
function sum(...numbers) {
return numbers.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3, 4));
// Rest in destructuring: take one property, keep the remainder
const { theme, ...others } = settings;
console.log(theme, others);
// Spread copies are shallow: nested arrays are still shared
const original = { name: "Ana", tags: ["js"] };
const copy = { ...original };
copy.tags.push("css");
console.log(original.tags);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Copy and extend an array
const extended = [...base, 4, 5];base ရဲ့ element တွေကို array အသစ်ထဲ ဖြန့်ထည့်ပြီး 4 နဲ့ 5 ကို ဆက်ထည့်ပါတယ်။ base ကိုယ်တိုင်ကတော့ [1, 2, 3] အတိုင်း ကျန်ပါတယ်။
- Array to arguments
console.log(Math.max(...base));Math.max က array ကို မလက်ခံဘဲ number တွေကို argument အဖြစ် တစ်ခုချင်း လိုချင်ပါတယ်။ Spread က Math.max(1, 2, 3) အဖြစ် ပြောင်းပေးလို့ 3 ရပါတယ်။
- Last property wins
const settings = { ...defaults, ...userPrefs };defaults ကို အရင်ဖြန့်ပြီး userPrefs ကို နောက်မှ ဖြန့်လို့ fontSize 18 က 14 ကို အစားထိုးပါတယ်။ အစဉ်ပြောင်းရေးရင် user ရဲ့ ရွေးချယ်မှု ပျောက်သွားပါမယ်။
- Rest parameter
function sum(...numbers) {sum(1, 2, 3, 4) ရဲ့ argument အားလုံးကို numbers ဆိုတဲ့ array အစစ်ထဲ စုလို့ reduce ကို တိုက်ရိုက် သုံးနိုင်ပါတယ်။
- Rest in a pattern
const { theme, ...others } = settings;theme ကို သီးသန့်ထုတ်ပြီး ကျန်တဲ့ property (fontSize) တွေကို others ဆိုတဲ့ object အသစ်ထဲ စုထည့်ပါတယ်။ Property တစ်ခုကို ဖယ်ထုတ်တဲ့ နည်းလမ်းကောင်းပါ။
- Shared nested array
copy.tags.push("css");copy က object အသစ်ဖြစ်ပေမယ့် tags က array တစ်ခုတည်းကို ညွှန်းနေဆဲပါ။ ဒါကြောင့် original.tags ထဲမှာပါ "css" ပေါ်လာပါတယ်။
[ 1, 2, 3, 4, 5 ]
3
{ theme: 'light', fontSize: 18 }
10
light { fontSize: 18 }
[ 'js', 'css' ]Nested data ကို immutable ပုံစံနဲ့ update လုပ်ခြင်း
Shallow copy ပြဿနာကို ရှောင်ဖို့ ပြောင်းချင်တဲ့ လမ်းကြောင်းတစ်လျှောက်က အဆင့်တိုင်းကို spread လုပ်ရပါတယ်။ အောက်က code မှာ notifications.email ကို ပြောင်းဖို့ အပြင် object ကို တစ်ခါ၊ notifications ကို တစ်ခါ spread လုပ်ထားပါတယ်။ မပြောင်းတဲ့ အပိုင်းတွေ (ဥပမာ profile) ကိုတော့ reference အတိုင်း မျှဝေထားလို့ memory လည်း သက်သာပါတယ်။
const state = {
profile: { name: "Ana" },
notifications: { email: true, sms: false },
};
const next = {
...state,
notifications: { ...state.notifications, email: false },
};
console.log(state.notifications.email, next.notifications.email);
console.log(next.profile === state.profile); // untouched part is shared
console.log(structuredClone(state).profile === state.profile);Array ကြီးကြီးကို argument အဖြစ် မဖြန့်ပါနဲ့
Math.max(...values) က argument တစ်ခုစီကို stack ပေါ်တင်ပါတယ်။ Element သန်းချီရှိတဲ့ array ဆိုရင် engine ရဲ့ ကန့်သတ်ချက်ကို ကျော်ပြီး RangeError တက်နိုင်ပါတယ်။ Data ပမာဏ ကြီးနိုင်တဲ့နေရာမှာ values.reduce((max, v) => Math.max(max, v), -Infinity) ကို သုံးပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
React၊ Redux လို UI library တွေက state ကို တိုက်ရိုက် မပြင်ဘဲ object အသစ် ပြန်ဆောက်ဖို့ လိုပါတယ်။ ဒါမှ "ဘာပြောင်းသွားလဲ" ဆိုတာကို reference နှိုင်းယှဉ်ရုံနဲ့ သိနိုင်လို့ပါ။ ဒါကြောင့် setUser({ ...user, name: "Mya" }) နဲ့ setItems([...items, newItem]) လို code ကို နေရာတိုင်းမှာ တွေ့ရပါမယ်။ Item ဖျက်တာကို filter နဲ့၊ update ကို map နဲ့ spread ပေါင်းပြီး ရေးပါတယ်။
Configuration စနစ်တွေမှာလည်း const config = { ...defaults, ...fileConfig, ...envConfig } လို အလွှာလိုက် ပေါင်းပါတယ်။ ညာဘက်ကျလေ ဦးစားပေးမှု မြင့်လေဖြစ်လို့ environment variable က file setting ကို၊ file setting က default ကို အနိုင်ယူပါတယ်။ ဒီအစဉ်ကို test တစ်ခုနဲ့ စစ်ထားတာ ကောင်းပါတယ်။
Rest parameter ကိုတော့ logger(level, ...messages) လို wrapper function တွေ၊ argument တွေကို နောက် function ဆီ fn(...args) နဲ့ ဆက်ပို့တဲ့ debounce/retry helper တွေမှာ အများဆုံး တွေ့ရပါတယ်။ const { password, ...safeUser } = user လို့ ရေးပြီး password ကို ဖယ်ကာ API response ပြန်ပို့တာလည်း အသုံးများပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Merge အစဉ်ကို ပြောင်းပြန်ရေးတာ
{ ...userPrefs, ...defaults } လို့ ရေးမိရင် default တွေက user ရဲ့ ရွေးချယ်မှုကို အမြဲ ပြန်ဖုံးသွားပါတယ်။ User က setting ပြောင်းလိုက်ပေမယ့် ဘာမှ မပြောင်းသလို ဖြစ်နေပါမယ်။ Default ကို ဘယ်ဘက်ဆုံး၊ override ကို ညာဘက်မှာ ထားပါ။
Spread က deep copy လို့ ထင်တာ
const copy = { ...state } ပြီးနောက် copy.items.push(x) လုပ်ရင် state.items ပါ ပြောင်းပါတယ်။ UI library တွေမှာ reference မပြောင်းလို့ screen ကလည်း update မဖြစ်ပါဘူး။ ပြောင်းမယ့် nested level တိုင်းကို spread လုပ်ပါ၊ ဒါမှမဟုတ် structuredClone() ကို သုံးပါ။
Rest ကို နောက်ဆုံးမှာ မထားတာ
function f(...items, last) ဒါမှမဟုတ် const [...start, end] = arr လို့ ရေးရင် SyntaxError တက်ပါတယ်။ Rest က "ကျန်တာအားလုံး" ဖြစ်လို့ နောက်မှာ ဘာမှ မကျန်တော့ပါဘူး။ နောက်ဆုံး element လိုရင် arr.at(-1) ကို သုံးပါ။
Object ကို array ထဲ ဖြန့်တာ
[...user] က user ဟာ iterable မဟုတ်လို့ TypeError တက်ပါတယ်။ Object ရဲ့ value တွေကို array အဖြစ် လိုချင်ရင် Object.values(user) ဒါမှမဟုတ် Object.entries(user) ကို သုံးပါ။
Mini challenge
Setting တွေကို immutable ပုံစံနဲ့ ပေါင်းပါ
Default setting တွေကို override object အရေအတွက် မသတ်မှတ်ထားဘဲ ပေါင်းပေးတဲ့ function တစ်ခုနဲ့ nested notifications ကို update လုပ်တဲ့ function တစ်ခုကို ရေးပါ။ ဘယ် input object ကိုမှ မပြောင်းရပါဘူး။
- mergeSettings(defaults, ...overrides) က override တွေကို ဘယ်ကနေ ညာ အစဉ်အတိုင်း ပေါင်းပါ
- setNotification(settings, channel, enabled) က notifications object ကိုပါ အသစ်ဆောက်ပါ
- withoutSecrets(settings) က apiKey ကို rest destructuring နဲ့ ဖယ်ထုတ်ပါ
- နောက်ဆုံးမှာ defaults ကို print ထုတ်ပြီး မပြောင်းကြောင်း ပြပါ
const defaults = {
theme: "light",
language: "en",
apiKey: "secret-123",
notifications: { email: true, sms: false },
};
function mergeSettings(defaults, ...overrides) {
// TODO: start from a copy of defaults and spread each override over it
}
function setNotification(settings, channel, enabled) {
// TODO: return a new object whose notifications is also a new object
}
function withoutSecrets(settings) {
// TODO: use rest destructuring to leave apiKey out
}
// TODO: merge { theme: "dark" } and { language: "my" },
// turn sms on, strip the secret, and print the resultsရလဒ်: ပေါင်းထားတဲ့ setting မှာ theme က dark၊ language က my ဖြစ်ပြီး sms က true ဖြစ်ပါမယ်။ apiKey မပါတဲ့ object ကို ပြပြီး နောက်ဆုံး defaults ကတော့ light၊ en၊ sms false အတိုင်း ကျန်နေပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
overrides.reduce((acc, o) => ({ ...acc, ...o }), { ...defaults }) လို reduce ကို သုံးနိုင်ပါတယ်။ Nested update မှာ notifications: { ...settings.notifications, [channel]: enabled } လို computed key ကို သုံးပါ။
အဖြေ ကြည့်ရန်
const defaults = {
theme: "light",
language: "en",
apiKey: "secret-123",
notifications: { email: true, sms: false },
};
function mergeSettings(defaults, ...overrides) {
return overrides.reduce((acc, override) => ({ ...acc, ...override }), { ...defaults });
}
function setNotification(settings, channel, enabled) {
return {
...settings,
notifications: { ...settings.notifications, [channel]: enabled },
};
}
function withoutSecrets(settings) {
const { apiKey, ...publicSettings } = settings;
return publicSettings;
}
const merged = mergeSettings(defaults, { theme: "dark" }, { language: "my" });
const updated = setNotification(merged, "sms", true);
console.log(withoutSecrets(updated));
console.log("defaults:", defaults.theme, defaults.language, defaults.notifications);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- ... က value နေရာမှာ spread (ဖြန့်)၊ နာမည်နေရာမှာ rest (စု) ဖြစ်ပါတယ်
- Object spread မှာ နောက်မှ ရေးတဲ့ property နိုင်လို့ default ကို အရင်၊ override ကို နောက်မှ ရေးပါ
- Rest parameter က array အစစ်ကို ပေးပြီး rest element က အမြဲ နောက်ဆုံးမှာ ရှိရပါတယ်
- Spread က shallow copy ဖြစ်လို့ ပြောင်းမယ့် nested level တိုင်းကို spread လုပ်ပါ
MDN — JavaScript Guide — MDN Web Docs