Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function A from function B within the same namespace?

Tags:

javascript

Let's say I have the namespace,

var Namespace = {
    A : function() {
        alert('Hello!');
    },
    B : function() {
        // Call A() from here, do other stuff
    }
}

In this namespace, I intend for A to be a helper function to B. That is to say, A() will never be called outside the namespace. It will only be called by the functions within the namespace.

What's the best way to address the issue of a local/helper function within a namespace? The way I see it there are two possibilities:

// Method #1    
var Namespace = {
    A: function() {
        alert('Method #1');   
    },

    B : function() {
        Namespace.A();
    }
}

Namespace.B();

// Method #2
function Namespace2() {
    var A = function() {
        alert('Method #2');
    };

    this.B = function() {
        A();        
    }
}

var ns2 = new Namespace2();
ns2.B();

In the first method, it is ugly and awkard to type Namespace.A() (repeatedly) in every function within the namespace. This leads me to prefer Method #2. But I was curious what was the best practice here.

like image 748
ktm5124 Avatar asked Jul 22 '13 15:07

ktm5124


People also ask

How do you call a function in namespace?

Namespaces in C++ You only need to prefix the function you wish to call with namespace_name:: -- similar to how you would call a static member function of a class. Another convenience of namespaces is that they allow you to use the same function name, when it makes sense to do so, to perform multiple different actions.

Can you call a function within another function?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can a function be called multiple times?

In order to run a function multiple times after a fixed amount of time, we are using few functions. setInterval() Method: This method calls a function at specified intervals(in ms). This method will call continuously the function until clearInterval() is run, or the window is closed.


3 Answers

I recommend placing the "namespace" inside a function scope. Everything not explicitly public will be naturally private:

var Namespace = (function() {
    var self = {};

    // Private
    var A = function() {
        ...
    };

    // Public
    self.B = function() {
        A();
    }

    return self;
}());

Namespace.B(); // Works
Namespace.A(); // Doesn't work
like image 104
Hubro Avatar answered Nov 08 '22 05:11

Hubro


You can call it using this statement

this.A();
like image 35
Yuriy Galanter Avatar answered Nov 08 '22 03:11

Yuriy Galanter


Well you can event use a third option where the Namespace is created in it's own scope:

var Namespace = (function(){
    var A = function() {
        alert('scoped method');
    };

    function Namespace() {

          var A1 = function() {
               alert('Namespace "private" method');
          };

          Namespace.prototype.B1 = function(){
              A();  //will run
              A1(); //will run with no errors
          };

    };

    Namespace.prototype.B = function(){
        A();  //will run
        A1(); //ERROR!
    };

    return Namespace;
})();
like image 23
Naftali Avatar answered Nov 08 '22 04:11

Naftali