admin管理员组文章数量:1022415
Suppose I have a javascript function of the form:
function(){alert("blah");}
Suppose I also have an applet, which has a method called doStuff that takes the function as a parameter:
MyApplet.doStuff(function(){alert("blah");});
Now suppose that the applet passes the function to a success or failure javascript callback function depending on the result of its calculations. In that callback function, I want to execute the function so that the user gets my exceedingly informative "blah" message:
function callback(func) {
func();
}
However, in the example above, func is no longer considered to be of type "function" (typeof func will return "object"). Is it possible to convert func to be a function so that it can be executed? I have a number of hacks in mind that can give me what I want, but they are very ugly and I was hoping that I was missing something simple.
Any help is greatly appreciated.
Suppose I have a javascript function of the form:
function(){alert("blah");}
Suppose I also have an applet, which has a method called doStuff that takes the function as a parameter:
MyApplet.doStuff(function(){alert("blah");});
Now suppose that the applet passes the function to a success or failure javascript callback function depending on the result of its calculations. In that callback function, I want to execute the function so that the user gets my exceedingly informative "blah" message:
function callback(func) {
func();
}
However, in the example above, func is no longer considered to be of type "function" (typeof func will return "object"). Is it possible to convert func to be a function so that it can be executed? I have a number of hacks in mind that can give me what I want, but they are very ugly and I was hoping that I was missing something simple.
Any help is greatly appreciated.
Share Improve this question edited Jan 25, 2020 at 17:23 Uwe Keim 40.8k61 gold badges190 silver badges304 bronze badges asked Dec 1, 2009 at 18:01 Hypnotic MeatHypnotic Meat 712 silver badges5 bronze badges 3-
I don't understand. How is
callback
itself passed around? – Crescent Fresh Commented Dec 1, 2009 at 18:09 -
What is the content of this object passed by applet? Can you inspect it in firebug? If it has a
call()
orapply()
function, you can try calling them. – Chetan S Commented Dec 1, 2009 at 18:11 - The name of the callback function itself is passed to the applet as a parameter. From there, I use the JSObject from the java plugin (netscape.javascript.JSObject) to call it and pass an object array containing the params. – Hypnotic Meat Commented Dec 1, 2009 at 19:10
3 Answers
Reset to default 3Try func.call()
Using the same style code as you already have, the following should alert "called!":
function callback(func){
func();
}
callback(function(){
alert("called!");
});
Is MyApplet.doStuff(function(){alert("blah");}); definitely passing its argument to the callback function unchanged?
I opted to go for a different solution that isn't as ugly as I imagined it would be. The JSObject.call() java method converts all the callback function parameters to plain jane objects. So instead of passing:
function(){alert("blah")}
I now pass:
alert("blah")
or after some annoying escaping:
''alert(\''blah\'');''
Not so terrible eh? Many thanks to everyone that offered their help, it is appreciated.
Suppose I have a javascript function of the form:
function(){alert("blah");}
Suppose I also have an applet, which has a method called doStuff that takes the function as a parameter:
MyApplet.doStuff(function(){alert("blah");});
Now suppose that the applet passes the function to a success or failure javascript callback function depending on the result of its calculations. In that callback function, I want to execute the function so that the user gets my exceedingly informative "blah" message:
function callback(func) {
func();
}
However, in the example above, func is no longer considered to be of type "function" (typeof func will return "object"). Is it possible to convert func to be a function so that it can be executed? I have a number of hacks in mind that can give me what I want, but they are very ugly and I was hoping that I was missing something simple.
Any help is greatly appreciated.
Suppose I have a javascript function of the form:
function(){alert("blah");}
Suppose I also have an applet, which has a method called doStuff that takes the function as a parameter:
MyApplet.doStuff(function(){alert("blah");});
Now suppose that the applet passes the function to a success or failure javascript callback function depending on the result of its calculations. In that callback function, I want to execute the function so that the user gets my exceedingly informative "blah" message:
function callback(func) {
func();
}
However, in the example above, func is no longer considered to be of type "function" (typeof func will return "object"). Is it possible to convert func to be a function so that it can be executed? I have a number of hacks in mind that can give me what I want, but they are very ugly and I was hoping that I was missing something simple.
Any help is greatly appreciated.
Share Improve this question edited Jan 25, 2020 at 17:23 Uwe Keim 40.8k61 gold badges190 silver badges304 bronze badges asked Dec 1, 2009 at 18:01 Hypnotic MeatHypnotic Meat 712 silver badges5 bronze badges 3-
I don't understand. How is
callback
itself passed around? – Crescent Fresh Commented Dec 1, 2009 at 18:09 -
What is the content of this object passed by applet? Can you inspect it in firebug? If it has a
call()
orapply()
function, you can try calling them. – Chetan S Commented Dec 1, 2009 at 18:11 - The name of the callback function itself is passed to the applet as a parameter. From there, I use the JSObject from the java plugin (netscape.javascript.JSObject) to call it and pass an object array containing the params. – Hypnotic Meat Commented Dec 1, 2009 at 19:10
3 Answers
Reset to default 3Try func.call()
Using the same style code as you already have, the following should alert "called!":
function callback(func){
func();
}
callback(function(){
alert("called!");
});
Is MyApplet.doStuff(function(){alert("blah");}); definitely passing its argument to the callback function unchanged?
I opted to go for a different solution that isn't as ugly as I imagined it would be. The JSObject.call() java method converts all the callback function parameters to plain jane objects. So instead of passing:
function(){alert("blah")}
I now pass:
alert("blah")
or after some annoying escaping:
''alert(\''blah\'');''
Not so terrible eh? Many thanks to everyone that offered their help, it is appreciated.
本文标签: javascriptCast from object to functionStack Overflow
版权声明:本文标题:javascript - Cast from object to function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745529599a2154686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论