admin管理员组

文章数量:1431037

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() or apply() 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
Add a ment  | 

3 Answers 3

Reset to default 3

Try 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