calling a javascript function (in original function) from called function?

后端 未结 2 833
迷失自我
迷失自我 2021-01-26 12:56

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 functi

相关标签:
2条回答
  • 2021-01-26 13:02

    I slightly changed the signature of your loadthis function aloowing it to be passed the order to actually 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.

    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();
    }
    
    0 讨论(0)
  • 2021-01-26 13:17

    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();
    }; 
    
    0 讨论(0)
提交回复
热议问题