What is the syntax for JavaScript / class namespaces inside the current namespace / class?

I have been working a lot with JavaScript lately and have created several large collections of functions. I found the need to have auxiliary functionality that I want to separate from the main library, but I will still be included in the main library, but I have not yet found an elegant enough way to do this.

Here are some examples that I am currently using.

So, I usually set up my main library as

// Main library
var animals = function(settings){
    // Main library stuff
}

To add helper classes / helper functions that are encapsulated separately but still part of the main library ...

This is the first method using object literal notation.

animals.prototype.dogs = {
    addDog: function(){
        // Can only access the dogs object
        var test = this;
    },
    removeDog: function(){}
}

// Usage of the whole thing
var animalsInstance = new animals({a: 1, b: 3});
animalsInstance.dogs.addDog();

, , dog.

animals.prototype.dogs = function(){
    var parent = this;
    return {
        addDog: function(){
            // The animals instance
            var test = parent;
            // The dogs instance
            var test2 = this;
        },
        removeDog: function(){}
    }
}

// Usage
var animalsInstance = new animals({a: 1, b: 3});
animalsInstance.dogs().addDog();

. , . - - ?

+5
1

, - ...

tinyFramework.Utilities.Storage.setItem() tinyFramework.Elements.Modals.createModal()

var tinyFramework = {

    Utilities: {
        Storage: {
            setItem: function(){
            //do stuff
            },

            getItem: function(){
            //do stuff
            }
        },
        Helpers: {

        }
    },

    Elements: {

        Modals: {

            createModal: function(){
            },
            closeModal: function(){
            }
        }
    }
}
+4

All Articles