开发者

Pass a defined function as a parameter

I am trying to pass in a function as a parameter:

GEvent.addListener(marker_1, "click",开发者_JAVA百科 populateMarkerWindow(0, marker_1) );

the addListener function takes an inline function as a parameter, and it works fine, but I want to call this function from other places as well, so I made it into a defined function:

var populateMarkerWindow = function(id, marker) {
  //...
}

This, however, only means that my function gets called when I try to register it.

Is there a way to tell JS that I want to pass in the function itself, not the result of the call to the function?

Thank you,


Just pass the function's name. If you need to arrange for the function to be invoked with a particular set of parameters, well, there are fancy ways to do that, but the simplest thing is to just wrap a call to your function in another function.

GEvent.addListener(marker_1, "click", function() {
  populateMarkerWindow(0, marker_1);
});

You might want to look for Douglas Crockford's various lectures on YouTube, as well as his website (crockford.com I think). In addition to that there are approx. a zillion other Javascript introductions out there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜