當我將一些開源ES5代碼合併到我的ES6類中時,我得到了「Uncaught TypeError:this.time_to_x不是函數」。這裏是班級(我已經刪除了一些散裝,但大部分基本的東西都在那裏)。假設調用Diamond()。這是這條線獲取錯誤:x = this.time_to_x(frame.time);類方法不是函數嗎?
爲什麼time_to_x()不被視爲函數?
export default class TimelinePanel {
constructor(ctx) {
this.ctx = ctx;
this.ctx_wrap = ctx;
}
create (ctx) {
this.rect({ctx, x: 20, y: 15, width: 130, height: 10}); // ***
this.drawLayerContents();
}
Diamond(frame, y) {
var x, y2;
x = this.time_to_x(frame.time);
y2 = y + LINE_HEIGHT * 0.5 - DIAMOND_SIZE/2;
var self = this;
var isOver = false;
this.path = function() {
this.ctx_wrap
.beginPath()
.moveTo(x, y2)
.lineTo(x + DIAMOND_SIZE/2, y2 + DIAMOND_SIZE/2)
.lineTo(x, y2 + DIAMOND_SIZE)
.lineTo(x - DIAMOND_SIZE/2, y2 + DIAMOND_SIZE/2)
.closePath();
};
}
drawLayerContents() {
// ...
for (i = 0; i < il; i++) {
// ...
for (j = 0; j < values.length; j++) {
// Dimonds
frame = values[j];
renderItems.push(new this.Diamond(frame, y));
}
}
}
y_to_track(y) {
if (y - MARKER_TRACK_HEIGHT < 0) return -1;
return (y - MARKER_TRACK_HEIGHT + scrollTop)/LINE_HEIGHT | 0;
}
x_to_time(x) {
var units = time_scale/tickMark3;
return frame_start + ((x - LEFT_GUTTER)/units | 0)/tickMark3;
}
time_to_x(s) {
var ds = s - frame_start;
ds *= time_scale;
ds += LEFT_GUTTER;
return ds;
}
}
你是怎麼調用'Diamond()'的? – 2017-02-18 02:20:48
請說明你是怎麼稱呼'鑽石'的。你很可能不會將「this」綁定到正確的對象。 – 4castle
我不知道它是否與你的問題有關,但是你在'Diamond'裏面定義'this.path'的方式是不正確的。這個函數裏面的'this'不會是你想要的。解決這個問題最簡單的方法是使用箭頭函數編寫'this.path =()=> {'。 – 2017-02-18 02:32:18