Mark
Mark

Reputation: 33571

javascript Running a function under another function

I have 3 functions, with the third function under the first that I want to run in the second.

function first () {
    function third () {
    }
}

function second () {
    third ();
}

How can I make second run third correctly?

Thanks.

Upvotes: 2

Views: 398

Answers (6)

Allen Rice
Allen Rice

Reputation: 19446

That depends on how you want to setup the first function and how you want to access it. Basically the third method is private to first. You'll need a public method on first to call third. That method would be called by second.

There are various ways to do this, one that comes to mind is this...

Edit: I named the parameters a little better, that way its not "param" over and over.

function first()
{
    // define first's private
    var third = function(third_param)
    {
        alert(third_param);
    }

    // define first's public that calls the private
    this.callThird = function (call_third_param)
    {
        third(call_third_param);
    }

}

function second ()
{
    // get an instance of first
    var temp = new first();

    // call the public method on it
    temp.callThird('argument');
}

If you don't care about third being private, you can do

function first() { }

// public method
first.prototype.third = function(third_param)
{
    alert(third_param);
}


function second ()
{
    // get an instance of first
    var temp = new first();

    // call the public method on it
    temp.third('argument');
}

or, this way, this is also not using privates

function first()
{
    // the "this", makes it public
    this.third = function(third_param)
    {
        alert(third_param);
    }

}

function second ()
{
    // get an instance of first
    var temp = new first();

    // call the public method on it
    temp.third('argument');
}

If you are trying to just do a namespace kind of thing, you can do this (no privates as well)

// define an object and name it first
var first =
{
    // give first a public variable but make it a function with an arg
    third : function(third_param)
    {
        alert(third_param);
    }

}

function second ()
{
    // call the third method on the first variable
    first.third('argument');
}

But probably the best way is via namespacing

var first = (function () {
    // define first's private function, store it in 'third', a private variable
    var third = function (third_param) {
        alert(third_param);
    };

    // return this object to the 'first' variable
    return { // define first's public that calls the private
        'callThird': function (call_third_param) {
            third(call_third_param);
        }
    };
} ());  // this line runs the function, closure'ing over the privates and 
// returning an object (that has access to the privates) to the 'first' variable

function second () {
    // usage:
    first.callThird("Hello World!");
}

Upvotes: 4

Ateş Göral
Ateş Göral

Reputation: 140050

Leaving aside all the "why would you want to do this?" questions, and assuming you need to do this for a legitimate reason like accessing a "private" function inside some existing code that you don't own, here's a hack:

first.toString() will give you the entire source of the function. You can inject a bit of code into that function to return you a reference to the third function:

var prober = new Function(
    first.toString().replace("{", "{ return third;") + "return first()");
var third = prober();
third(); // yay!

But, please don't do this if you really don't need to. It's a big hack that I just wanted to mention for educational purposes.

Upvotes: 0

allyourcode
allyourcode

Reputation: 22603

Make first return third. This suggestion makes more sense if your example looks more like this:

function first(foo) {  // notice that foo is local to first
  function third() {
    // do something with foo in the outer scope
  }
  return third;
}

function second(bar) {
  bar(); // call whatever gets passed in
}

second(first());  // third (created inside first) gets passed to second

If you don't have any variables that are local to first (unlike my example, where I used foo), there's no point in not defining third in the global namespace i.e. you should do this instead:

function first() {
}

function second() {
  third();
}

function third() {
}

Upvotes: 0

James Black
James Black

Reputation: 41858

Your best bet is to pull the function out that is common to both, and just call it where needed.

var thirdf = function third () {
}

function first () {
  thirdf();
}

function second () {
  thirdf();
}

Upvotes: 0

kafuchau
kafuchau

Reputation: 5593

Why don't you make third() a function not nested under first(), and then just have first() and second() call third()?

e.g.

function first(){
   third();
}

function second(){
   third();
}

function third(){

}

Upvotes: 0

CodeJoust
CodeJoust

Reputation: 3790

Javascript won't easily allow it, why would you need to do that in the first place, you might want to use objects or prototypes to accomplish this.

function first(){
   third();
}

function second(){

}

function third(){
  second();
}

Upvotes: 0

Related Questions