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