Reputation: 3
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
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