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
2 answers

Why when javascript namespacing do we say var myName = myName || {}

I cannot find the reason behind this format? I assume it is to check if this variable has been created before and if so, to take that one rather than the blank object. But I cannot think HOW or WHY this would already exist when I am assigning…
Todd Vance
  • 4,627
  • 7
  • 46
  • 66
0
votes
1 answer

Can we add the same namespace for two javascript files?

I am trying to put all my javascript function in one namespace. I have two javascript files and i am trying to put all the functions both the files into one namespace. So when i define the same namespace for both the files. The first gets over…
user2208533
  • 37
  • 2
  • 5
0
votes
1 answer

Not able to call a function in a namespace from the html file

I have two JavaScript file which has some functions within a name space. But when i try to invoke this function from my main page it is throwing an error telling "Object # has no method 'LoadAllBooks'"... But I have invoked the the function by using…
user2208533
  • 37
  • 2
  • 5
0
votes
2 answers

inheritance namespace javascript

How can I inherit an objects variables within namespace(scope)? var f2 = { a: 'test' } f2.history = { load: function(){ alert(this.a); } } // Turns out Undefined f2.history.load();
Adrian McCool
  • 143
  • 2
  • 12
0
votes
5 answers

Inheritance in nested namespaces in JavaScript

I want to setup nested namespaces to organize my code. I am trying to follow the plugin-like structure described in this article. The problem is I don't understand how to access this.error_msg in my example. Have I set this up correctly? Would I…
Justin
  • 26,443
  • 16
  • 111
  • 128
0
votes
3 answers

How to append to a JavaScript namespace created from a closure?

I have a namespace in the following format allowing for public and private members: function A() { return('a'); } namespace1 = (function () { // private namespace2 = (function() { // private prC = function () { …
CoryG
  • 2,429
  • 3
  • 25
  • 60
0
votes
1 answer

How assign value to a namespace object variable if it is get it inside a jquery on click definition

I want to know how can I save a value inside a jquery bind when this is inside a namespace. var obj = obj || (obj ={ valuetosave:-1, init:function(){ $("some selector").on("click",function(){ tmpval = I get some value…
raulricardo21
  • 2,499
  • 4
  • 20
  • 27
0
votes
2 answers

Why don't jQuery selector variables work within namespaces?

I am stumped on why this is not working. I'm looking to having more than one variable declared. What am I doing wrong? var message = (function ($) { var $modalBody = $('.modal-body'), $lblToUser =…
Davis
  • 2,937
  • 3
  • 18
  • 28
0
votes
1 answer

Knockout validation & namespacing

I'm having trouble combining namespacing with knockout validation. This breaks the validation: myNameSpace = { viewModel: { name: ko.observable().extend({ digit: { digit: true, message: "digits only"} }) …
0
votes
2 answers

IIFE jquery ready

I am working on a web project that has a large amount of javascript and we started hitting namespace collisions because we were adding everything to "$.". I read up about namespacing and found the great article at…
user1601333
  • 151
  • 1
  • 10
0
votes
0 answers

Javascript Sub Module Method

I recently started at an environment where the sub module method of JS programming is used exclusively throughout the application. Coming from the wild west of advertising however, I am having a hard time wrapping my head around it. I understand the…
collin
  • 240
  • 1
  • 3
  • 12
0
votes
1 answer

backbone.js model is not a costructor into router

I have a problem with a backbone.js namespacing app demo, i'm trying to display a simple list of element stored locally, when i call the app the console retrieves this error App.model.club is not a constructor the portionof code is in the router…
0
votes
1 answer

Namespace issue when calling function in one .js file from another .js file

In an attempt to make a swipe-able slideshow for many images, I am attempting to stitch together two plugins, Swipe.js and lazyloader.js. I want to have the slideshow timer event from the Swipe plugin call the update function inside of Lazyloader.…
mbuckl1
  • 3
  • 5
-1
votes
1 answer

Nested functions error, Unexpected token ':', Cannot read property of undefined

I want to make my code more clear, that the reason i am trying to learn namespaces or nested functions. I've read about them in several places and as i understood 'Revealing Module' is the best option. So i tried to copy second example from this…
-1
votes
1 answer

Is it possible to arrange Java Script files in to 'name spaces' to avoid conflicts?

I'm building a website using a premium html template, part of the template is in Javascript. So in the template I have something like this - its in the 'master template' every page. //Template JS
Ayo Adesina
  • 2,231
  • 3
  • 37
  • 71
1 2 3
10
11