⛔️ Object.assign causing TypeError
Object.assign(target, ...sources) 裡面 source 物件的 methods 似乎不是可以任意寫的,當內部使用物件方法時,JS 引擎(還是 Node 環境❓)竟然會檢查型別❗️
下面的範例用了三個方法來產生一個二維陣列的「最長列長」:
✅ 使用全域函數 (global functions)
✅ 使用 Object.defineProperty()
❌ 使用 Object.assign()
但用最後一種方法 (mixin) 會出問題❗️
💊 解藥: .assignDescriptors()
// Array.prototype + .max(), .maxRowLength (by Object.assign)
Object.assign(Array.prototype, {
// max something in the array
max(mapf = (x) => x) {
// -----------------------------------------
return Math.max(...this.map(x => mapf(x)));
// ^^^^^^^^
// ⛔ TypeError: this.map is not a function
// -----------------------------------------
},
// max row length of the matrix
get maxRowLength() {
return this.max(row => row.length);
},
});
// matrix (2D array)
let m = [[1, 2, 3, 4], [1, 2], [1, 2, 3]];
m.max(row => row.length)
m.maxRowLengthexplanation: replit
Last updated
Was this helpful?