0

I'm trying to separate my JavaScript into nice libraries. I have 2 companies under the net top-level-domain (net.foxbomb and net.matogen)

var net = {
    foxbomb : {
        'MyObject' : function() {
            console.log ("FoxBomb")
        }
    }
}
var net = {
    matogen : {
        'MyObject' : function() {
            console.log ("Matogen");
        }
    }
}
var f = new net.foxbomb.MyObject();
var m = new net.matogen.MyObject();

Of course, I've just defined two nets - which doesn't work. What is the correct way to do this?

1
  • This has nothing to do with the concept of "packages," e.g. see npmjs.org. Editing for clarity. Commented Apr 24, 2012 at 21:44

2 Answers 2

2

File 1:

var net = net || {};

net.foxbomb = {

  // ...

};

File 2:

var net = net || {};

net.matogen = {

  // ...

};

Le fiddle: http://jsfiddle.net/Q8TnL/1/

Sign up to request clarification or add additional context in comments.

4 Comments

I'm assuming you want to separate the scripts into different files. If I'm reading you wrong, let me know and I'll delete this.
I just tried an example with them both in the same file (they would typically be in different files), but it's not working :(
@MarkvanWyk is it org.foxbomb or net.foxbomb?
Apologies GGG, I made a typo. Works 100%. Thanks for that!
2

Separate the properties by a comma:

var net = {
    foxbomb : {
        'MyObject' : function() {
            console.log ("FoxBomb")
        }
    }, // <-- Comma
    matogen : {
        'MyObject' : function() {
            console.log ("Matogen");
        }
    }
};

1 Comment

I forgot to mention that they're in different JavaScript files.

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.