这是我的代码:
function active_area(width, t_width, height) {
this.width = width;
this.height = height;
this.t_width = t_width; //width of toolbar
this.dotes = 20;
this.gridStep = this.width/this.dotes;
this.active_layer = -1;
this.layers_array = [];
}
active_area.prototype.init = function () {
this.canvas = createCanvas(this.width+this.t_width, this.height);
this.canvas.parent('mapper');
this.addButton(10, 10, 'Zoom out', this.zoomOut);
this.addButton(10, 50, 'Zoom in', this.zoomIn);
this.addButton(30, 30, 'Add layer', this.addLayer);
this.drawGrid();
};
active_area.prototype.addButton = function(x, y, name, func){
var pos_x = this.width+x;
var add_layer = createDiv();
add_layer.position(pos_x, height + y);
add_layer.html('<paper-button>'+name+'</paper-button>');
add_layer.mouseClicked(func);
};
active_area.prototype.zoomOut = function () {
console.log('zoom out');
console.log('dotes:'+this.dotes);
this.dotes = this.dotes*2;
console.log('dotes:'+this.dotes);
redraw();
//this.drawGrid();
};
方法问题zoomOut:我无法从类active_area访问this.dotes,drawGrid无法访问。这个方法调用精细,监听器工作,但如何访问类范围?
答案 0 :(得分:0)
你需要绑定它
this.addButton(10, 10, 'Zoom out', this.zoomOut.bind(this));
this.addButton(10, 50, 'Zoom in', this.zoomIn.bind(this));
this.addButton(30, 30, 'Add layer', this.addLayer.bind(this));