从被调用的函数调用一个javascript函数(原函数)? [英] calling a javascript function (in original function) from called function?

查看:118
本文介绍了从被调用的函数调用一个javascript函数(原函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,从另一个函数调用一个函数..有点难以解释。继承人的例子。一个函数加载html页面,并在准备就绪时调用原始函数。

我想我需要传入一个引用,但不确定如何执行此操作...如果我设置了它到这 - 它似乎并没有工作



任何想法?

  order.prototype.printMe = function(){
order_resume.loadthis(myTestPage.html,showData);
}

order.prototype.testme = function(){
alert(我被称为);
}

//然后在loadthis中需要调用

orderRsume.prototype.loadthis = function(){
// DO SOME的东西,负载时其到达OnReady
}

order.prototype.OnReady =函数(){
///需要在其他功能$ b CALL ORIGINAL TESTME
$ b


解决方案

我不清楚你真的想做。在JS中,函数是头等对象。因此,您可以将函数作为参数传递给另一个函数:

  Cook(龙虾,
water ,
函数(x){alert(pot+ x);});






  order .somefunc = function(){
//做东西
}

order.anotherone =函数(func){
//做东西和调用函数func
func();
}

order.anotherone(order.somefunc);






如果您需要引用未命名的函数它是正文,下面的语法应该可以工作:

  order.recursivefunc = function f(){
//你可以使用f仅在此范围内,afaik
f();
};


Is there anyway to calling a function from another function .. little hard to explain. heres in example. One function loads html page and when ready it calls the original function.

I think i need to pass in a reference but unsure how to do this... if i set it to "this" - it doesn't seem to work

ANy ideas?

order.prototype.printMe = function(){
    order_resume.loadthis("myTestPage.html", "showData");
}

order.prototype.testme= function(){
     alert("i have been called");
}

//Then when in "loadthis" need to call 

orderRsume.prototype.loadthis= function(){
    //  DO SOME STUFF AND WHEN LOADS IT ARRIVES IN OnReady
}

order.prototype.OnReady= function(){
  /// NEED TO CALL ORIGINAL "testme" in other function
}

解决方案

It's not clear for me what you really want to do. In JS functions are first-class objects. So, you can pass function as a parameter to another function:

Cook("lobster", 
     "water", 
     function(x) { alert("pot " + x); });


order.somefunc = function(){
    // do stuff
}

order.anotherone = function(func){
    // do stuff and call function func
    func();
}

order.anotherone(order.somefunc);


And if you need to refer to unnamed function from it's body, following syntax should work:

order.recursivefunc = function f(){
    // you can use f only in this scope, afaik
    f();
}; 

这篇关于从被调用的函数调用一个javascript函数(原函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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