简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM