views:

401

answers:

2

Hi there,

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
}
+3  A: 

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();
};
alex vasi
thank you very ..
mark smith
A: 

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