JavaScript Knowledge point Record (iii) design pattern

Source: Internet
Author: User

JavaScript design pattern Recording, this aspect really is not written, the work also does not use the JS design pattern place.

Single-Case mode

Ensure that a class has only one instance and provides a global access point to access it. For example: thread pool, global cache, login floating window.

First we need to extract the logic code of the Singleton, and then use the lazy Singleton, that is, the return method. Execution occurs only when clicked.

A single example of JavaScript is different from a class. Instead of creating redundant constructors, you can create global variables directly.

! (function () {    //Manage the logic code of a single case, if no data is created, the data is returned   varGetsingle =function(FN) {//parameter is the method for creating the object       varresult; return function(){//judgment is null or assigned           returnResult | | (Result = Fn.apply ( This, arguments));   };    }; //To Create a login window method    varCreateloginlayer =function(){        vardiv = document.createelement (' div '); Div.innerhtml= ' I am logged in to the floating window '; Div.style.display= ' None ';        Document.body.appendChild (DIV); returnDiv;    }; //Single Case Method    varCreatesingleloginlayer =Getsingle (Createloginlayer); //use lazy singleton to createdocument.getElementById (' loginbtn '). onclick =function(){        varLoginlayer =Createsingleloginlayer (); LoginLayer.style.display= ' Block '; };}) ()
Policy mode

Define a series of algorithms to encapsulate them one by one. The use of the algorithm is separated from the implementation of the algorithm.

JavaScript's strategy model is simple, and the algorithm is directly defined as a function.

! (function () {    //Defining algorithm Methods    varStrategies = {        "S":function(Salary) {returnSalary * 4; },        "A":function(Salary) {returnSalary * 3; },        "B":function(Salary) {returnSalary * 2;    }    }; //Execution Algorithm    varCalculatebouns =function(level,salary) {returnStrategies[level] (salary);    }; Console.log (Calculatebouns (' S ', 2000));}) ()

Easing algorithm

Placeholder

JavaScript Knowledge point Record (iii) design pattern

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.