user3284007
user3284007

Reputation: 1717

Creating namespaces in JavaScript

I'm kind of new to JavaScript. I have a bunch of utility functions I've written over the years in C#. I'd like to migrate some of them to JavaScript. When I use them in code, as a developer, I'd like to make the following call:

myCompany.myLibrary.functionName(param1, param2, etc);

My question is, how do I set this up in JavaScript? For instance, at this time, I have:

var functionName = function() {
  // do stuff
};

However, I'm not sure how to 'scope' that into myCompany.myLibrary. Is this type of thing possible. I think it is. I'm basing this off of libraries like lodash. However, as I'm kind of new to JavaScript, I might be incorrect in my interpretation.

Thank you for any help you can provide.

Upvotes: 2

Views: 114

Answers (4)

hex494D49
hex494D49

Reputation: 9225

Just another way how you could organize your JavaScript library (and have your own namespace at the same time).

var myCompany = myCompany || {};

myCompany.myLibrary = (function() {

    var myPrivateProperty = "I'm a private property.";
    var myPrivateMethod = function () {
    // I'm a private method
    }

    return {
        myPublicProperty: "I'm a public property.",
        myPublicMethod1: function () {
        // I'm a public method
        },
        myPublicMethod2: function (arg1, arg2) {
            // I'm a public method, capable to return/call private property or method 
            return myPrivateProperty; 
        }
    };
})();

Usage:

myCompany.myLibrary.myPublicProperty;    
myCompany.myLibrary.myPublicMethod1();
myCompany.myLibrary.myPublicMethod2(x,y);

Upvotes: 0

Dick van den Brink
Dick van den Brink

Reputation: 14499

With the code below you can extend the "namespace" from different files. I believe this is called the module pattern.

var myCompany;
(function (myCompany) {
    (function (myLibrary) {
        myLibrary.someFunction = function () {
        };
    })(myCompany.myLibrary || (myCompany.myLibrary = {}));
    var myLibrary = myCompany.myLibrary;
})(myCompany || (myCompany = {}));

Upvotes: 1

Strille
Strille

Reputation: 5781

I like the approach where you have a "namespace" function that takes the string "myCompany.myLibrary" in your case and creates that object structure (if it doesn't exist) and returns a reference to the object:

namespace('myCompany.myLibrary').functionName = function(){}

You can find an example of how the namespace function could be written here: http://elegantcode.com/2011/01/26/basic-javascript-part-8-namespaces/

Upvotes: 0

Jim Cote
Jim Cote

Reputation: 1756

var myCompany = {
    myLibrary1: {
        func1: function (param1, param2, etc) {...},
        func2: function (param1, etc) {...},
        prop1: 7,
        prop2: 'xyz'
    },
    myLibrary2: {
        func1: function () {...},
        prop1: 123.0004
    }
};

Then you can:

myCompany.myLibrary1.func1(1, 2, 3);
myCompany.myLibrary2.prop1 = 'hello world';

Upvotes: 0

Related Questions