这实际上是对我之前的问题Access instance variable inside a function in javascript?的后续问题。访问原型方法内的匿名函数内的实例变量
我想访问原型方法内的匿名函数内的实例变量。上述
function MyObject(){
//Instance variables
this.handler;
}
//Methods
MyObject.prototype.enableHandler = function(){
var button = document.getElementById('button');
button.onclick = function(){
this.handler();//Is not working
}
}
var myObject = new MyObject();
myObject.handler = function(){
alert('Hello World!');
}
myObject.enableHandler();
的jsfiddle http://jsfiddle.net/3cmvZ/
的例子只是为了阐明如何可以访问一个实例变量的匿名函数内部的原型方法内。我已经知道button.onclick = this.handler
的作品。
忘了提及的例子只是为了澄清我如何可以访问一个实例变量的匿名函数内部的原型方法中。我已经知道'button.onclick = this.handler'作品 – einstein
@ Woho87 - 查看更新。 –
如果我更改'$ this.handler',this.handler是否也会被更改?' – einstein