2

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.

3
  • 1
    There are no namespaces, what you want is just an object accessed with dot notation. Commented Jun 22, 2014 at 19:44
  • See if this helps developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Jun 22, 2014 at 19:45
  • 1
    Look into AMD modules rather than simply namespacing your code. There are far more benefits. Commented Jun 22, 2014 at 20:08

4 Answers 4

1

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 = {}));
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0
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';

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.