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
0
votes
3 answers

How to fire multiple namespace functions at once?

Is it possible to execute all the functions of a namespace with one call? Exp: var myApp = { e : $('.js-box'), addStyle : function(){ myApp.e.css('height','200'); }, warn : function(){ alert('WOOOOOoooOO'); } …
Mar
  • 1,526
  • 6
  • 21
  • 46
0
votes
2 answers

How to use javascript namespaces?

I am trying to figure out JavaScript namespaces... I would like to write couple of functions and secure them in namespaces like below.. but I think I don't get it very well..This is too simple question but it will be clear for me I guess If I see…
Mar
  • 1,526
  • 6
  • 21
  • 46
0
votes
1 answer

JavaScript Namespace objects : script include order

I seem to run into a problem with name spacing in JavaScript. I have separated my javascript objects into separate files. Each file starts with namespace definition: var MySystem = MySystem || {}; If I include the object, which calls some methods…
Spencer Mark
  • 5,263
  • 9
  • 29
  • 58
0
votes
1 answer

Typescript Class Store

Maybe I am not using the namespaces correctly with Typescript but here is the problem I am trying to solve. When creating a class, I need to reference all my other files by using a require as followed. import DependencyModule =…
user2465083
  • 595
  • 3
  • 12
0
votes
0 answers

JavaScript namespace and IE8, null or not an object error

I am working on an application which was supposed to be compatible with modern browsers but now we have a requirement that it should support older browsers like IE8, IE9.. I have followed namespace pattern while developing an app. Like the…
0
votes
1 answer

How to assign the read the root var in JS independently of context (Browser, Node)?

I have a function that assigns an object to a custom namespace, without overwritting existing objects in the path, a bit like mkdir -p, for example: assignObjectToNamespace(myObj, "com.stackoverflow.questions.root", window) The prototype would is…
bitoiu
  • 6,893
  • 5
  • 38
  • 60
0
votes
0 answers

Are there any measurements of the performance hit incurred from global namespace pollution in JavaScript?

One of the first things I learned when writing JavaScript was that it bad practice to set variables on window and polluting the global namespace. Looking around there are loads of articles as there are SO questions on why it is considered bad…
frequent
  • 27,643
  • 59
  • 181
  • 333
0
votes
0 answers

How to load through RequireJS a non-AMD script that manipulates the Global Namespace?

I have some Javascript modules and their dependencies declared and organized with RequireJS, and it works like a charm. However, one specific module calls a function defined into the Global Namespace by a legacy non-AMD script - which I'm loading…
viniciushana
  • 235
  • 1
  • 2
  • 12
0
votes
2 answers

Is it possible to pass namespace as parameter in javascript function?

I have 3 .js files. The main Home.js and two other .js files, for instance Page1.js, Page2.js Home.js: var Home= { Sample: function (pageId,data) { pageId.MergePageData(data); } } Page1.js: var Page1 = { …
Xavier
  • 1,672
  • 5
  • 27
  • 46
0
votes
1 answer

Javascript modular namespace pattern

I'm trying to use a modular namespace pattern which allows me to expand the functions available within various namespaces and define them across multiple files. This is the pattern I'm using; var Namespace = Namespace || {}; Namespace.SubSpace =…
Red Taz
  • 4,159
  • 4
  • 38
  • 60
0
votes
2 answers

JavaScript function - Global Namespace

In Javascript, there exists a function which inherits the Backbone Model window.MyModel = Backbone.Model.extend({ .. .. }); window.MyCollection = Backbone.Collection.extend({ .. .. }); In another, JS file we access this function as var…
Ravi G
  • 859
  • 1
  • 8
  • 21
0
votes
1 answer

How can I use a Namespace in two different files?

I'm trying to use a Namespace in two different files. First file: $(document).ready(function () { var App= window.App || {}; App.Form = can.Control.extend({... }); window.App = App; …
Glund
  • 507
  • 1
  • 5
  • 20
0
votes
1 answer

add namespace to your javascript

I have worked on google map api few years ago and wrote a little reusable utility. At that time adding google map api reference does add all the api classses in your page's global namespace. As this is working sample
0
votes
0 answers

Is this a good way to create a namespace in javascript?

I have been reading a bunch of posts, questions, and articles on the use of the with keyword in JavaScript. Some of the articles warn against the use of Javascript's with keyword, so I'd like to know if my use is appropriate. Most of my programming…
Isioma Nnodum
  • 1,318
  • 13
  • 13
0
votes
1 answer

Reusing a knockout.js viewModel without using global variables

I have a page with different knockout generated content. I have a right menu with data that should load new data in the middle of the page depending the row chosen in the right menu. So, I can fetch the content in the middle of the page. It loads…
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168