TrackTech
TrackTech

Reputation: 3

Global Namespace Javascript Error

var NS = NS|{};  
NS.A = {
prop1: 'hello',
prop2: 'there',
func: function() {alert('boo');}  
}; 
NS.A.func()

The above code gives NS.A is undefined error. If I declare NS as below it works

var NS = {};

Help me understand why.I am trying to use global namespace as defined in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

Upvotes: 0

Views: 206

Answers (1)

Alex R
Alex R

Reputation: 624

This actually should work, you just have a typo in the first line.

You have: var NS = NS|{};

You should have: var NS = NS||{};

This article explains this kind of evaluation

Upvotes: 1

Related Questions