我只想將類(Pixel)的對象添加到數組。TypeScript將對象添加到數組與推
export class Pixel {
constructor(x: number, y: number) {}
}
類具有以下屬性:
pixels: Pixel[] = [];
下面的代碼看起來合乎邏輯的我,但實際的對象並沒有推送到我的像素陣列。
this.pixels.push(new Pixel(x, y));
只有這樣工作的:
var p = {x:x, y:y};
this.pixels.push(p);
任何人可以解釋我爲什麼上面的語句不起作用?
你的代碼是罰款和運作良好時,我嘗試它。它必定是導致問題的其他因素。請包括有問題的真實代碼以及錯誤消息 –