Questions tagged [javascript-namespaces]

JavaScript namespaces provide a level of direction to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name.

In order to reduce the number of objects and functions that are added to the global scope in our applications, using namespaces in JavaScript is definitely a recommended practice. Just like static members, namespaces don’t have any dedicated syntax built into the language either. But we’re able to get the same benefits by creating a single global object and adding all our objects and functions to this object. This way we lower the possibility of naming collisions when using our code in conjunction with other JavaScript libraries. We can also use the same naming conventions for namespaces as in any other programming language that does provide syntactical support.

154 questions
1035
votes
27 answers

How do I declare a namespace in JavaScript?

How do I create a namespace in JavaScript so that my objects and functions aren't overwritten by other same-named objects and functions? I've used the following: if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();} Is there a more…
Scott McKenzie
  • 15,338
  • 7
  • 42
  • 67
46
votes
5 answers

How to set up JavaScript namespace and classes properly?

It seems there are so many ways to set up a JavaScript application so it is confusing as to which one is correct or best. Are there any difference to the below techniques or a better way of doing this? MyNamespace.MyClass = { someProperty: 5, …
34
votes
5 answers

What does this "(function(){});", a function inside brackets, mean in javascript?

Possible Duplicates: What does this mean? (function (x,y)){…}){a,b); in JavaScript What do parentheses surrounding a JavaScript object/function/class declaration mean? Hi All I don't know what the following does: (function(){ // Do something…
Shaoz
  • 10,223
  • 25
  • 64
  • 98
25
votes
4 answers

What is meant by 'JavaScript Namespacing'?

Possible Duplicate: Javascript Namespacing Im pretty new to JavaScript and was wondering if anyone could give me a good description of what is meant by JavaScript Namespacing? Also any resources e.g. articles etc, are much appreciated on the…
bc17
  • 313
  • 1
  • 3
  • 6
15
votes
3 answers

How do you write DRY, modular coffeescript with Sprockets in Rails 3.1?

I'm in the early stages of trying to write some sensible Javascript. I want to namespace basically everything under the name of my application to avoid globals as much as possible, but still give me a way to access functions declared around the…
12
votes
4 answers

How to turn on the 'throwIfNamespace' flag in React.js

I have some code like below in my component.
abu abu
  • 4,662
  • 6
  • 48
  • 93
11
votes
3 answers

How can I export from a Meteor package into my app's namespace?

I know how to write Meteor packages but I can't seem to figure out how to have all exports land in my app's namespace, as described in this presentation. This particular package is specific to an app I'm building, and it exports only one method that…
10
votes
2 answers

Namespace a dynamically loaded javascript file's contents

Is it possible to namespace a JavaScript file inserted dynamically? I know that I can dynamically include a JavaScript file by creating a script tag and insert it into the DOM, but can this included file be namespaced? So, if the file has a…
William Niu
  • 15,553
  • 7
  • 49
  • 92
8
votes
5 answers

Javascript Namespace - Multiple Levels

I'm currently doing the following to give my javascript code a namespace: (function(foo, $, undefined) { // function: showNoteDialog foo.showNoteDialog = function() { // ... } }(window.foo = window.foo || {}, jQuery)); What I…
Dismissile
  • 30,816
  • 34
  • 167
  • 253
8
votes
1 answer

JSDoc: What is a relationship between modules and namespaces

I faced a problem with understanding the purpose of namespaces and modules in a union. For example I have a class Game.utils.Matrix. I want to annotate Game as a namespace, utils as a module and Matrix as a class: /** * @namespace Game */ /** *…
aspirisen
  • 775
  • 9
  • 27
6
votes
3 answers

Javascript: How to clear a non-global (closured) setTimeout?

I'm trying to be a good citizen and keep as much out of the global scope as possible. Is there a way to access setTimeout variables that are not in the global scope? So that, in this example how would someone cancel 'timer'? myObject.timedAction =…
6
votes
4 answers

Javascript namespacing with RequireJS, why?

I'm currently facing this argument about namespaces on javascript and I need a community opinion. The scenario: The architect in charge of this project somehow is devoted to RequireJS and really wants to use it. I must say that the application is a…
AlexCode
  • 3,775
  • 4
  • 29
  • 46
5
votes
2 answers

JS namespace Netbeans variable not declared

When I was defining a namespace in JavaScript, Netbeans gave a global variable not declared error while I think it shouldn't. Why is this? Demonstration code: var MyNamespace = new function () { var MyClass = function () {}; …
SWdV
  • 1,135
  • 1
  • 11
  • 25
5
votes
2 answers

How to span javascript namespace across multiple files?

I have ignored javascript forever. I started using jQuery a few years back so I could get by. But as I've started doing TDD more I decided yesterday to really dive into javascript (and possibly coffeescript after that). In my ASP.NET Web Forms…
Chad Carter
  • 1,378
  • 1
  • 13
  • 14
4
votes
2 answers

OOP, private functions inside a top level method of the namespace

I have a js script which declares a namespace, then has a method called run() which I can call from inside a XUL script like myNamespace.run(): var myNamespace = { run: function() { var selectedText = getSelText(); alert…
bobbyrne01
  • 5,464
  • 13
  • 60
  • 127
1
2 3
10 11