昨天剛提到類別表示的方式,今天接續相關的內容
如果想在通用物件的類別實體裡表達例外屬性
但又讓非例外物件從通用物件的屬性取得標準值,覆蓋就是個實用的技巧
示例如下:
class CookieRecipe {
constructor(type, ingredient) {
this.type = type;
this.ingredient = ingredient;
this.favor = "sweet"; // the property of prototype is sweet
}
bake(time, temperature) {
console.log(
`The ${this.type} cookies need to bake at ${temperature} degrees for ${time} minutes.`
);
}
}
let cheeseCookie = new CookieRecipe("cheese");
cheeseCookie.favor = "salty"; // cover up the property of prototype
console.log(cheeseCookie.favor); // salty
這裡的 map 指的是資料結構的一種,不是 array.map() 的 map method
Map 也可稱為 associative array, dictionary,可視為鍵值對的組合,其最小單位為鍵值對組(key-value pair),每個鍵值(key)都是唯一的,並映射到對應的特定值
早期的 js 並沒有正式的 associative array,最接近的選項是 Plain Object,現在則可以用 modern js 的 Map class 來建立 Map 物件
雖然 plain object 可用來表示 map 概念,但跟 class Map 建立出來的 map 仍有部分差異
另外 Map 可用 for..of
來 iterate,但 Map 並非屬於 array
是因為 Map 是 iterable object 的一種,而 iterable object 包含 Map, array, nodeList
以下為 plain object 與 Map 的差異:
feature | Plain Object(as associative array) | Map |
---|---|---|
Keys 鍵值 | 只能是 string 或 symbol | 可為任何資料,如numbers, functions, objects,另外鍵(key)只有唯一,但可以多個不同鍵搭配相同值 |
Methods | 可用 [] 跟 . 取得屬性值 |
.get() |
Iterator Method | 普通的物件本身不像 Map, array 可迭代,只能用特殊方法如 for...in Object.entries() Object.keys() 遍歷物件的可枚舉屬性 |
for...of |
Build-in methods | Object.keys() |
.get() ,.delete() ,size() , |
Size | Object.keys() 獲取 length |
size |
Inheritance | 所有物件都會繼承來自 Object.prototype 的屬性,導致可能會有問題(像是預設繼承物件的 toString 方法) | Map 是一種特殊的資料集合,因此沒有繼承屬性 |
如果不想繼承來自Object.prototype的屬性,其實還有個方法
稍微回憶昨天的內容,有些值『本身沒有原型,自然就不會有繼承屬性跟方法』的問題
答案就是使用 Object.create(null)
答案是不可以
Object 並沒有像 array 或 Map 有內建 [Symbol.iterator]
的方法
另外 for...in
, Object.entries()
, Object.keys()
這些方法只能迭代物件內**『可枚舉(enumberable)』的屬性值**
物件的屬性必須是字串,如果要建立的 map 其 key 不容易轉換成字串(例如:物件),則不能使用物件作為 map
這時候可以使用 new Map()
建立 Map,用nameOfMap.set( nameOfKey , value)
來設定值
示例如下:
let ages = new Map();
ages.set("Benson" , 39);
ages.set("Julia" , 28);
ages.set("Emma" , 33);
nameOfMap.get( nameOfKey )
nameOfMap.delete( nameOfKey )
nameOfMap.size
nameOfMap.has( nameOfKey )
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map