它说没有定义this.draw,但我在同一个类中定义了绘图。为什么我不能在另一个类函数中调用一个类函数?
function Recorder() {
this.recording = false;
this.sideLength = 5;
this.currList = new SLinkedList(comparator);
this.curr = null;
}
Recorder.prototype = {
constructor:Recorder,
draw : function (xPos, yPos) {
if(recording) {
currList.addEnd([xPos,yPos]);
}
let context = document.getElementById("canvas").getContext("2d");
let getColorPickerByID = document.getElementById("colors");
let getValueOfColorPicker = getColorPickerByID.options[getColorPickerByID.selectedIndex].text;
context.fillStyle = getValueOfColorPicker;
context.fillRect(xPos,yPos,sideLength,sideLength);
},
processMousePosition : function (evt){
this.draw(evt.pageX, evt.pageY);
}
};
发布于 2019-04-04 17:11:45
为类提供一个名为handleEvent
的方法。使此函数检查evt.type
并为该事件调用适当的方法。
function Recorder() {
this.recording = false;
this.sideLength = 5;
this.currList = new SLinkedList(comparator);
this.curr = null;
}
Recorder.prototype = {
constructor:Recorder,
handleEvent : function(evt) {
switch (evt.type) {
case "mousemove":
this.processMousePosition(evt);
break;
}
},
draw : function (xPos, yPos) {
// your draw code
},
processMousePosition : function (evt){
this.draw(evt.pageX, evt.pageY);
}
};
然后,当您将侦听器添加到元素时,传递您的Recorder
实例而不是它的方法。这将导致在事件发生时调用handleEvent
方法。
var r = new Recorder();
myElement.addEventListener("mousemove", r);
https://stackoverflow.com/questions/55526903
复制