Namespace mode:
- Advantages: Can solve the naming confusion and third-party conflict;
- Cons: Long nesting results in longer query times; more characters;
- Universal namespace functions:
var MyApp = MyApp | | {}; Myapp.namespace = function (ns_string) { var parts = ns_string.split ('. '); parent = MYAPP; if (parts[0] = = = ' MYAPP ') { parts = parts.slice (1); } for (var i = 0; i < parts.length; i++) { if (typeof parent[parts[i]] = = = ' undefined ') { PARENT[PARTS[I]]
= {}; } Parent = Parent[parts[i]]; } return parent; var module2 = myapp.namespace (' MYAPP.modules.module2 ');
- Declaring dependencies: Although a modular naming has been made, the declaration of dependency can be more optimized
For: MYAPP, MYAPP.util.Event, myapp.util.dom;//at the top of a function or module var myFunc = function () { var Event = MYAPP.util.Event; The speed of parsing local variables is faster than resolving global variables;
- Private properties and Privileged methods
function Gadget () { var name = ' IPod '; This.getname = function () { return name; }}Note that if the private property setting is an object and is read-only, do not return it directly, but return its shallow copy;
JavaScript optimized--06 mode (object)