JavaScript Common Design Patterns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.