我无法从嵌套函数到达我的对象。我只是向前走,并显示代码。
function ChartObject(parent){
this.CANVAS = document.createElement('canvas');
parent.appendChild(this.CANVAS);
this.CANVAS.addEventListener('mousemove', function(evt){
var ctx = this.CANVAS.getContext("2d");//cannot access the CANVAS like this
//do stuff
}, false);
}
好吧我的问题是我无法从该行访问canvas对象: var ctx = this.CANVAS.getContext(" 2d"); //无法像这样访问CANVAS
答案 0 :(得分:0)
在绑定到this.CANVAS
的事件处理程序内部,画布将只是this
,因为它是绑定元素
function ChartObject(parent){
this.CANVAS = document.createElement('canvas');
parent.appendChild(this.CANVAS);
this.CANVAS.addEventListener('mousemove', function(evt){
var ctx = this.getContext("2d");
}, false);
}