通过反射的形式调用javascript函数?在jquery中
我有两个javascript函数:通过反射的形式调用javascript函数?在jquery中,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有两个javascript函数: function some_name(say_something){ console.log('hello' + say_something); } call_other_fuction_after_doing_something('some_function') // In some other file, some where - in the land of make believe. function call_other_fuction
function some_name(say_something){
console.log('hello' + say_something);
}
call_other_fuction_after_doing_something('some_function')
// In some other file, some where - in the land of make believe.
function call_other_fuction_after_doing_something(function_name){
$.ajax({
url : /*Go some where*/,
type : 'POST'
data : { /*Some kind of data*/},
success : function(result){
if(function_name !== undefined){
$.fn[success_action](result); // Pay attention to this!
}
},
});
}
正如我们可以看到的,我在一些文件中做了一些函数,其中一些记录到控制台:“hello”,然后它会有一个从附加到它的ajax调用返回的结果
当我读到一些关于这方面的文章时,我认为这是可行的。但显然我错了,因为我得到的错误是:
Uncaught TypeError: Object [object Object] has no method 'some_name'
你知道如何在javascript(jquery)中进行这种“反射”吗?你要做的是调用一个函数,这个函数是
$.fn的属性,它看起来不存在
尝试传递函数引用而不是名称
function some_name(say_something){
console.log('hello' + say_something);
}
call_other_fuction_after_doing_something(some_name)
//In some other file, some where - in the land of make believe.
function call_other_fuction_after_doing_something(fn){
$.ajax({
url : /*Go some where*/,
type : 'POST'
data : { /*Some kind of data*/},
success : function(result){
if(typeof fn == 'function'){
fn(result); // Pay attention to this!
}
},
});
}
为什么你要传递函数名,为什么你不能传递函数引用它自己呢?除了拼写错误,有问题的函数,如果我做一个console.log(fn)在if语句的外面,而在里面,我得到的只是外部返回:一些函数名
…你能做一个console.log吗(fn类型)
在之外,如果
返回并显示字符串。我是通过引用传递的,正如您的示例中所述,而不是通过字符串名称。@KyleAdams否…您是以字符串形式传递的…否则它应该返回函数I legit Coppid your example,用我的东西替换url和数据,并运行它并获得字符串而不是函数