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.

156 questions
2
votes
2 answers

js singleton - how to avoid accessing class members via the namespace

I'm trying to improve my Javascript coding style and have been reading that it's good to namespace stuff. However I can't seem to use the "this" keyword everywhere that I would like to - instead I can only access my class properties via the…
Xoundboy
  • 827
  • 15
  • 26
2
votes
3 answers

Problems with "this" in JavaScript namespaces and event listeners

First of all, I'm attempting to use faux namespaces in my JavaScript program like so: // Ish.Com namespace declaration var Ish = Ish || {}; Ish.Com = Ish.Com || {}; // begin Ish.Com.View namespace Ish.Com.View = new function() { var…
Ishmael Smyrnow
  • 942
  • 1
  • 9
  • 27
2
votes
1 answer

Trouble instantiating p5.js code (instance mode)

Update: Question has been solved. Here's the working instantiated code in case anybody needs it for help/reference: https://editor.p5js.org/Rod1C/sketches/iaKn9CKCS I'm new to p5.js and have been trying to load multiple sketches onto a web page.…
2
votes
0 answers

Understanding this in module/namespace and arrow functions

After some research on how to write modular/namespace in javascript, I saw that the following might be a good idea on how to encapsulate a function/object: var MODULE = (function (parent) { var OB = parent; var TH = this; …
jack
  • 1,391
  • 6
  • 21
2
votes
1 answer

How do I use backbone.js with namespaces?

I have been trying to get namespaces to work with backbone.js for the last hour or so. I have read: How do I declare a namespace in JavaScript? And I tried all approaches. Here is the problem: Backbone.Controller wants to be initialized through a…
Blub
  • 13,014
  • 18
  • 75
  • 102
2
votes
2 answers

How to correctly import the same namespace in two different files in Typescript

I'm having an issue in understanding how to make two files with the same namespace and import one into the other in Typescript. This is the situation: file1.ts export namespace nspace { export class ClassA { } } file2.ts import {nspace}…
valepu
  • 3,136
  • 7
  • 36
  • 67
2
votes
2 answers

Namespace issue in google closure library

In the tutorial mentioned here, the namespace provided by the module is: goog.provide('tutorial.notepad.Note'); But I am wondering why not this: goog.provide('tutorial.notepad'); Since, according to the rule mentioned below: tutorial = tutorial ||…
2
votes
2 answers

Why base a javascript namespace as a function?

I was reading the source code of a library, which must remain anonymous, and I see that it's using an empty function to setup the namespace. It appears to be similar to the object literal notation (OLN) except that the base is a function. Here's an…
alex
  • 756
  • 4
  • 12
2
votes
2 answers

How can I create my own namespace for global variables in Meteor?

I'm running into difficulty with understanding the scope of a variable that I thought would be global. Rather than figuring it all out, I thought maybe it would be better to declare my own global namespace and keep my stuff in there. Is this the way…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
2
votes
0 answers

How do I assign modules to namespaces in jsdoc?

We're using jsdoc for a very javascript-heavy project. In order to get code-documentation we let jsdoc build html-files via grunt. So far we successfully assigned properties and methods to modules using the "memberof" keyword like this: Example /** …
Timo Ernst
  • 15,243
  • 23
  • 104
  • 165
2
votes
1 answer

javascript issue with namespace, "this" and a library

I'm building a visualization page (with dc.js) for which I decided to make the jump and gather it all into a single namespace. Coming from Python's simplicity, crashing against JavaScript's scope madness has been tough enough, so please bear with…
Luis E.
  • 841
  • 11
  • 15
2
votes
2 answers

Same variables in different '