Singleton mode: Ensures that a class can only be instantiated once.
var obj = {}2, function return value var func = function () {return {}}var obj = func (); 3, constructor initialize var obj = (function () {return {}}) ()
Decorator mode: An object used by decorators to wrap the same interface.
var obj = obj | | {}obj.set = function () {}obj.get = function () {}obj.......= function () {}
Module Mode: This mode uses closures to encapsulate private states and organizations.
var module = (function (obj) {}) ({});
Observer pattern: It defines a one-to-many relationship that allows multiple observer objects to listen to a Subject object at the same time.
function func () {}func.prototype.set = function (opt) {}func.prototype.get = function (opt) {}var obj = new func (); Obj.set ({ }); Obj.get ({});
Constructor mode: Customize your own constructor, and then declare the properties or methods of the custom type object inside.
1. Constructor function func (name,age) {this.id = 0;//code ...} Func.prototype.pro = function () {}2, constructor enforces the instantiation of function func (title) { if (!) ( This instanceof func) { return new Func (title); } This.title = title;} Func.prototype.get = function () {return this.title;} Console.log (Obj.get ());
Factory mode: Factory mode is like real-life factories can produce a lot of similar products.
function func (opt) {var obj = {id:0,title: '}return $.extend (obj,opt);} var F1 = func ({id:1,title: ' title 1 '}), var F2 = func ({id:2,title: ' Title 2 '});
Object creation mode: Objects in object creation
Pattern 1: Namespace (namespace) var obj = obj | | {};obj.app = Obj.app | | {};obj.app.ios = Obj.app.ios | | {};obj.app.android = Obj.app.android | | {}; Mode 2: Create object var obj by self-executing function (function () {obj = {}}) mode 3: Chain mode var obj = {func1:function () {return this;},func2:function () { Return this,},......: Function () {return this;}} The chain method calls Obj.func1 (). FUNC2 (). (); Mode 4: function syntax sugar function syntax sugar is an extension of the method (function) for an object to be quickly added, mainly using the prototype attribute if (typeof Function.prototype.method!== "Function") { Function.prototype.method = function (name, implementation) { this.prototype[name] = implementation; return this; };} var func = function (name) { this.name = name;}. Method (' Set ', function (name) { this.name = name; }). Method (' Get ', function () { return this.name;}); var a = new Func (' a '); A.set (' B '); Console.log (A.get ());
Sandbox mode
JavaScript common Design Patterns