当我将一些开源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