我这样做但它不起作用:
function timer(func){
//Timer code here
...
func();
}
function doSomething(){
var param1;
var param2;
var execute = function(){
alert(param1 + " - " +param2);
};
}
var instance = new doSomething();
instance.param1 = "Hi";
instance.param2 = "Test";
timer(instance.execute);
为什么不是我的实例功能"执行"在定时器功能里面执行?我收到以下错误:Uncaught TypeError: func is not a function
什么是正确的方法?
答案 0 :(得分:1)
你的代码中有很多错误,我认为这是你想要做的事情
function timer(func){
func();
}
// following your constructor
function doSomething(){
var outer = this; //'this refer to current object'
this.execute = function(){
alert (outer.param1 + " - " +outer.param2); // its a closure function
};
return this;
}
var instance = new doSomething();
instance.param1 = "Hi";
instance.param2 = "Test";
timer(instance.execute);