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
4
votes
4 answers

Preferred technique for javascript namespacing

My code is going to turn into a mess if I don't start using some sort of namespacing technique. I'm relatively new to programming large javascript projects but have significant experience with systems programming in C++/java/python etc. Basically…
rewolf
  • 5,561
  • 4
  • 40
  • 51
4
votes
4 answers

Javascript Namespace - Is this a good pattern?

Objectives... Remove vars, objects etc from the global object. Remove possibility of collisions. Firstly I implement the Yahoo namespace code (note for example purposes I am using ROOT as the root of my namespace)... if (typeof ROOT ==…
Remotec
  • 10,304
  • 25
  • 105
  • 147
4
votes
5 answers

Define a class with namespace in Javascript

Refer to https://stackoverflow.com/a/387733/418439, // Define a class like this function Person(name, gender){ // Add object properties like this this.name = name; this.gender = gender; } // Add methods like this. All Person objects…
Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87
4
votes
1 answer

Javascript namespacing - how to export functions and variables defined within a function scope based on their naming?

With the code below, other than specifying manually, is there a way to export only the functions and variables whose name doesn't start with an underscore? var myapp = myapp || {}; myapp.utils = (function() { var CONSTANT_A = "FOO", …
tamakisquare
  • 16,659
  • 26
  • 88
  • 129
4
votes
2 answers

Using Bootstrap & Fancybox in a single project

I am using Bootstrap and Fancybox within a single project, including it like this:
3
votes
2 answers

How to best emulate namespaces in Typescript?

I realize that namespaces are essentially deprecated in ES6 and don't work well in Visual Code. But I still want to use them. Why? Because I have code spread across multiple files that I want to group without creating dependencies between…
3
votes
1 answer

TypeScript: Substitute Namespaces with something else

TSLint complains that namespaces shouldn't be used and as far as I understand the common sense is that they shouldn't be used anymore as they are special TypeScript construct. So, I have a simple Timestamp interface: export interface Timestamp { …
user3612643
  • 5,096
  • 7
  • 34
  • 55
3
votes
1 answer

accessing my public methods from within my namespace

I am in the process of making my own namespace in JavaScript... (function(window){ (function(){ var myNamespace = { somePublicMethod: function(){ }, anotherPublicMethod: function(){ } …
Derek Adair
  • 21,846
  • 31
  • 97
  • 134
3
votes
4 answers

JavaScript Namespace & jQuery Event Handler

I have created a Javascript namespace to avoid conflict with other Javascript codes. var ns = { init: function() { $('a').click(this.clickHandler); }, clickHandler: function() { // Some code here .. // The keyword "this"…
Ammar
  • 2,387
  • 1
  • 14
  • 12
3
votes
3 answers

Calling a Javascript function from a hash value with namespaces

I currently have this code to call a function from a hash value on page load: $(function() { var hash = window.location.hash.substring(1); window[hash](); }); This works great. However, my Javascript in namespaced like so: var help = { …
Hanna
  • 10,315
  • 11
  • 56
  • 89
3
votes
1 answer

jquery namespace: how to pass default options from one method to the subsequence methods?

How can I pass the default options from one method to the subsequence methods? For instance, I have some default setting in the init method. and I want to use these settings for other methods such as show, (function($) { var methods = { …
Run
  • 54,938
  • 169
  • 450
  • 748
3
votes
1 answer

Javascript namespaces that use ||

I've seen namespaces in JavaScript defined as: var AppSpace = AppSpace || {}; and/or var namespace = {}; Can anyone tell me: What's the difference? What's || used for in the first example? Why, in the first example, is AppSpace used twice? Which…
Adam Davies
  • 2,742
  • 4
  • 33
  • 52
3
votes
1 answer

jQuery Plugin Authoring and Namespacing

I'm used to writing plugins like so: ;(function($){jQuery.fn.myPlugin=function(options){ var defaults={ 'property':value }, o=$.extend({},defaults,options||{}); // INSERT AND CACHE ELEMENTS var $Element=$('
'); …
Aaron
  • 2,482
  • 1
  • 26
  • 56
3
votes
3 answers

Namespacing pattern causes JSLint 'function was used before it's defined' error

I am using the following namespacing pattern: var MyNamespace = new function () { var foo = function () { bar(); }; var bar = function () { alert("bar"); }; this.init = function () { foo(); …
Flash
  • 15,945
  • 13
  • 70
  • 98
2
votes
2 answers

how to pass variable to Javascript Namespace

I have created a Javascript namespace like so: var MyApp = function() { return { someFunction : function(x) {} alert(MyApp.y); } }(); This allows me to do this: MyApp.someFunction(x); I would like to be able to do…
Jorre
  • 17,273
  • 32
  • 100
  • 145
1
2
3
10 11