调用JavaScript函数通过指针参数? [英] Invoke Javascript function with argument via pointer?

查看:124
本文介绍了调用JavaScript函数通过指针参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如:

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

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

FOO(酒吧,process_json);
 

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

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

解决方案
  

在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

这篇关于调用JavaScript函数通过指针参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆