从被调用的函数调用javascript函数(在原始函数中)?

时间:2021-06-22 16:45:00

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.

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

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
}

2 个解决方案

#1


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:

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

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();
}; 

#2


I slightly changed the signature of your loadthis function aloowing it to be passed the order to actually load.

我略微改变了你的load这个函数的签名,让它传递给实际加载的命令。

I also assumed that your doSomeStuff function accepts a callback function. I assumed that it may be an AJAX call so this would be trivial to call a callback function at the end of the AJAX call. Comment this answer if you need more info on how to fire this callback function from your AJAX call.

我还假设你的doSomeStuff函数接受一个回调函数。我假设它可能是一个AJAX调用,所以在AJAX调用结束时调用回调函数是微不足道的。如果您需要有关如何从AJAX调用触发此回调函数的更多信息,请评论此答案。

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

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

//Then when in "loadthis" need to call 

orderRsume.prototype.load = function(order, page, action){
    //  DO SOME STUFF AND WHEN LOADS IT ARRIVES IN OnReady
    doSomeStuff(page, action, function()
    {
        order.OnReady();
    });
}

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

#1


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:

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

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();
}; 

#2


I slightly changed the signature of your loadthis function aloowing it to be passed the order to actually load.

我略微改变了你的load这个函数的签名,让它传递给实际加载的命令。

I also assumed that your doSomeStuff function accepts a callback function. I assumed that it may be an AJAX call so this would be trivial to call a callback function at the end of the AJAX call. Comment this answer if you need more info on how to fire this callback function from your AJAX call.

我还假设你的doSomeStuff函数接受一个回调函数。我假设它可能是一个AJAX调用,所以在AJAX调用结束时调用回调函数是微不足道的。如果您需要有关如何从AJAX调用触发此回调函数的更多信息,请评论此答案。

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

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

//Then when in "loadthis" need to call 

orderRsume.prototype.load = function(order, page, action){
    //  DO SOME STUFF AND WHEN LOADS IT ARRIVES IN OnReady
    doSomeStuff(page, action, function()
    {
        order.OnReady();
    });
}

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