调用JavaScript函数通过指针参数?指针、函数、参数、JavaScript

由网友(At last 终于)分享简介:你怎么可以调用一个JavaScript函数,同时传递参数,使用函数指针?例如:函数foo(一,回调){jQuery.post('/不久/ check.json',{VAR:一个},功能(RESP){回调(RESP);});}功能process_json(RESP){//做些事情RESP}FOO(酒吧,process...

你怎么可以调用一个JavaScript函数,同时传递参数,使用函数指针?

例如:

 函数foo(一,回调){
        jQuery.post('/不久/ check.json',{VAR:一个},功能(RESP){
             回调(RESP);
    });
}

功能process_json(RESP){
  //做些事情RESP
}

FOO(酒吧,process_json);
 

process_json永远不会被调用。展望萤火虫,字符串process_json是越来越传递到富,但我以为这再presents一个函数指针process_json。

在Javascript中,是不可能通过指针调用函数和传递参数?

解决方案   Axure RP 8 教程 数字与指针函数之开关侧边菜单

在Javascript中,是不可能通过指针调用函数和传递参数?

这肯定是有可能做到这一点。关于你的code一切看起来就好了我。你确定了 $。员额()回调(匿名函数)被调用?为不确定时,被调用?

  

要澄清一下,我们需要使用字符串来调用一个函数 - 不是函数指针。这可能吗?

是的。如果函数是全局定义的,你可以调用它的窗口属性的对象,像这样:

 函数foo(){/ *剪断* /}

VAR fn_name ='富';

window.foo(); // 作品
窗口['富'](); // 作品
窗口[fn_name](); //也适用
 

How can you invoke a function in Javascript, while passing in arguments, using a function pointer?

Example:

function foo (a, callback) {    
        jQuery.post('/soon/check.json', { var:a }, function(resp) {
             callback(resp);
    }); 
}

function process_json(resp) {
  // Do something with resp
}

foo(bar, process_json);

"process_json" never gets invoked. Looking in Firebug, the string "process_json" is getting passed into "foo," but I assumed this represents a pointer to the function "process_json."

In Javascript, is it not possible to invoke functions via pointers and pass in arguments?

解决方案

In Javascript, is it not possible to invoke functions via pointers and pass in arguments?

It most certainly is possible to do this. Everything about your code looks just fine to me. Are you sure that the $.post() callback (the anonymous function) is being called? Is bar undefined when foo is invoked?

To clarify, we need to invoke a function using a string -- not a function pointer. Is this possible?

Yes. If the function is defined globally, you can invoke it as a property on the window object, like so:

function foo () { /* snip */ }

var fn_name = 'foo';

window.foo();       // works
window['foo']();    // works
window[fn_name]();  // also works

阅读全文

相关推荐

最新文章