Introduced
This article mainly introduces the pattern of creating objects, using a variety of techniques to greatly avoid errors or to write very concise code.
Mode 1: Namespace (namespace)
Namespaces can reduce the number of global naming needs and avoid naming conflicts or excesses. Usually when we do object-level definition, this is often the case:
Copy Code code as follows:
var app = App | | {};
App.modulea = App.modulea | | {};
App.moduleA.subModule = App.moduleA.subModule | | {};
App.moduleA.subModule.MethodA = function () {
Console.log ("Print A");
};
App.moduleA.subModule.MethodB = function () {
Console.log ("Print B");
};
If there's a lot of hierarchy, it's going to go on and on, it's confusing. namespace mode is to solve this problem exists, we look at the code:
Copy Code code as follows:
Unsafe and may overwrite existing MyApp objects
var MYAPP = {};
OK
if (typeof MYAPP = = "undefined") {
var MYAPP = {};
}
In a more concise way
var MYAPP = MYAPP | | {};
Defining common methods
Myapp.namespace = function (ns_string) {
var parts = ns_string.split ('. '),
Parent = MYAPP,
I
Default if the first node is MyApp, ignore it, such as Myapp.modulea
if (parts[0] = = "MYAPP") {
Parts = Parts.slice (1);
}
for (i = 0; i < parts.length i + + 1) {
If the property does not exist, it is created
if (typeof parent[parts[i]] = = = "undefined") {
Parent[parts[i]] = {};
}
Parent = parent[parts[i]];
}
return to parent;
};
Call code, very simple:
Copy Code code as follows:
After namespace, you can assign the return value to a local variable
var module2 = myapp.namespace (' MYAPP.modules.module2 ');
Console.log (Module2 = = = MYAPP.modules.module2); True
Skip MyApp.
Myapp.namespace (' modules.module51 ');
Very long name.
Myapp.namespace (' Once.upon.a.time.there.was.this.long.nested.property ');
Mode 2: Defining dependencies
Sometimes one of your modules or functions may have to refer to a third party's modules or tools, so it's best to define them at the beginning so you can easily replace them later.
Copy Code code as follows:
var myfunction = function () {
Dependent modules
var event = YAHOO.util.Event,
dom = YAHOO.util.dom;
Local variables event and Dom are used in the code behind other functions
};
Mode 3: Private properties and Private methods
JavaScript This book does not provide a specific syntax to support private and private methods, but we can do this by closing the code as follows:
Copy Code code as follows:
function Gadget () {
Private objects
var name = ' IPod ';
Public functions
This.getname = function () {
return name;
};
}
var toy = new Gadget ();
Name is not defined, is private
Console.log (Toy.name); Undefined
Public method Access Name
Console.log (Toy.getname ()); "IPod"
var myobj; Assigning values to myobj by a self-executing function
(function () {
Free objects
var name = "My, oh I";
Realized the public part, so no Var
MyObj = {
Authorization method
Getname:function () {
return name;
}
};
} ());
Mode 4:revelation mode
It's also about the pattern of hiding private methods, and the module pattern in the deep understanding of the JavaScript series (3): Fully parsing module mode is somewhat similar, but not the way of return, but rather the declaration of a variable externally, and then assigning a public method internally to the variable. The code is as follows:
Copy Code code as follows:
var myarray;
(function () {
var astr = "[Object Array]",
toString = Object.prototype.toString;
function IsArray (a) {
Return Tostring.call (a) = = = Astr;
}
function indexOf (haystack, needle) {
var i = 0,
max = haystack.length;
for (; i < Max; i + 1) {
&nbs p; if (haystack[i] = = needle) {
return i;
}
}
return-1;
}
By assigning value, all of the above details are hidden.
MyArray = {
Isarray:isarray,
Indexof:indexof,
Inarray:indexof
};
} ());
Test code
Console.log (Myarray.isarray ([1, 2])); True
Console.log (Myarray.isarray ({0:1})); False
Console.log (Myarray.indexof (["A", "B", "Z"], "Z")); 2
Console.log (Myarray.inarray (["A", "B", "Z"], "Z")); 2
Myarray.indexof = null;
Console.log (Myarray.inarray (["A", "B", "Z"], "Z")); 2
Mode 5: Chain mode
Chain mode you can call a method of an object in succession, such as Obj.add (1). Remove (2). Delete (4). Add (2) in such a way that it is very simple to implement, that is, to return this as is. The code is as follows:
Copy Code code as follows:
var obj = {
Value:1,
Increment:function () {
This.value + 1;
return this;
},
Add:function (v) {
This.value + V;
return this;
},
Shout:function () {
Console.log (This.value);
}
};
Chain method call
Obj.increment (). Add (3). Shout (); 5
You can also call a single
Obj.increment ();
Obj.add (3);
Obj.shout ();
Summary
This is the object creation mode of the previous article, please look forward to tomorrow's next article.