JavaScript command pattern principle and usage examples

This paper describes examples of JavaScript command mode principle and usage. Share to you for your reference, as follows:

First, command mode:
(1) for eliminating the direct coupling mode between the caller and the receiver, and can (call this process the traces of the operation)

(2) really do not mess with this mode, you simply call that he makes writing very complex and somewhat difficult to understand.

(3) the emergence of your business (back operation) (redo) needs to be considered when you use this mode of.
Principle command:

A case where the issuer is directly applied to the performer, so that a high degree of coupling, as another case, the issue and executor of Add a storage room with access to the command of the command library that is command mode.

Second, now we learn by a demand for the model

1 there is a “Process button to add” single hit when a new text will be added as the process described in

2. there are “back”, “redo” button 2 to complete the corresponding task.
Third, an interface to


effects, JavaScript命令模式原理与用法实例详解

FIG we step through the above
step a, defined main application —- receiver

  function manager ( ) {this.addFlow = function (idValue) {//1. Get the target node var div = document.getlementByid ("DIV01"); var newflow = document.createElement ("div"); newflow.setttribute ("id", id); newflow.innerhtml = Value; div.appendchild (newflow);}}  
Step 2, establish a command access library for the object (executor) – meaning can be accessed through the EXTCUTE method AddFlow Method

Manager.Prototype.extcute = (Function () {/ * Command Command Object * * / Return Function (Command) {Return this [Command.Method], Command.Value);}}) ();

Step 3, initializing the main class
var ma = new manager (); // You can use this object, call it // for storing “” Call Object command “collection varcomances = new array (); // Collection cursor – initialization at the end Var index = Commands.Length;

Step 4, the client —- issuer
 VAR API = function () {this.addflow = function () {// Calling the encapsulation Var command = {Method: "addflow", ID: new uuid (). CreateUuID (), // Generate ID VALUE: Document.getElementByid ("flow"). Value}; // Save the call object Get up, used to roll back and re-act, commands.push (command); // reposition the cursor --- assignment record index = commands.Length; // call m.extcute (command);}; / ** * The method returned * / this.ret = function () {if (index-1  = commands.length) {Alert ("has arrived in front, not to do ...");} Else {var command = command [index]; // Get the current command position ma.extcute (command); index = index + 1;}}}  

Step 5, Instantiated client

API = New API (); // Instantiation
Event in HTML CanTogether.
Here we use plugins to allow its function to support custom keyboard events, plugin names: KeyMaster.js

First, in HTML, the file is introduced,
Then value the value to add a custom keyboard event
// Add Support Ctrl + Z – Return Key (“Ctrl + Z”, Function () {API.RET ();}); // Ren — Key (“Ctrl + Shift + X”, function () {api.again ();})

To this end we can use the designated combination of the keyboard to achieve the same effect as the mouse click.

Here, you will need to explain the ID value in the client’s API, but also through the plugin to dynamically generate —— plug-in name: uuid.js. Here, the source code

/ * uuid.js – Version 0.2javascript Class to create a uuid Like IdentifierCopyRight (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved.This program is free software; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USAThe latest version of this file can be downloaded fromhttp: / / 6/5/06 – Initial Release5 / 22/08 – Updated Code To Run Faster, Removed Randrange (MAX) IN FAVOR OF A Simpler Rand (MAX) Function. Reduced overhead by using gettime () method of date class (suggestion by james Hall).KNOWN ISSUES: – Still no way to get MAC address in JavaScript- Research into other versions of UUID show promising possibilities (more research needed) – Documentation needs improvement * /// On creation of a UUID object, set it’s initial valuefunction UUID ( ) { = this.createuuId ();} // start what this object is, lie and return it’s valueuuiD.Prototype.Valueof = function () {return this.iD;} uuid.prototype.tostring = function ( ) {return;} //// INSTANCE SPECIFIC METHODS // UUID.prototype.createUUID = function () {// // Loose interpretation of the specification DCE 1.1: Remote Procedure Call // described at http: // // Since JavaScript Doesn’t Allow Access To Internal Systems, The Last 48 BitS // of the node section is master Up Using a series of random Numbers (6 OCTES long). // var DG = New Date (1582, 10, 15, 0, 0, 0); var DC = New Date (); var t = dc.gettime () – DG.GetTime (); var h = ‘-‘; var tl = uuid.getintegerbits (t, 0, 31); var TM = uuid.getintegerbits (t, 32, 47); VAR THV = uuid.getintegerbits (t, 48, 59) + ‘1’; // version 1, security version is 2 var csar = uuid.getintegerbits (uuid.rand (4095), 0, 7); var CSL = uuid.getintegerbits (uuid.rand (4095), 0, 7); // Since Detection of anything About the machine / browser is far to buggy, // incrude some more random numbers here // if nic or an ip can Be Obtained Reliably, That Should Be Put In /// Here Instead. var n = uuid.getintegerbits (uuid.rand (8191), 0, 7) + uuid.getintegerbits (uuid.rand (8191), 8,15) + uuid.getintegerbits (uuid.rand (8191), 0, 7) + uuid.getintegerbits (uuid.rand (8191), 8, 15) + UUID.Getintegerbits (uuid.rand (8191), 0, 15) ; // this Last Number IS Two octets long return TL + H + TM + H + THV + H + CSAR + CSL + H + N;} //// General Methods (Not Instance Specific) //// Pull Out Only certain bits from a very large integer, used to get the time // code information for the first part of a UUID. Will return zero’s if there // are not enough bits to shift where it needs to.UUID.getIntegerBits = function ( Val, start, end) {var base16 = uuid.returnbase (VAL, 16); var quadarray = new array (); var quadstring = ”; var i = 0; for (i = 0; i = base Var output = this.returnBase (MSD, BASE) + Convert [LSD]; Else Var Output = Convert [MSD] + Convert [LSD];} Return Output;} // Pick a Random Number Withnin A Range of Numbers // Int B Rand (INT A); WHERE 0
   Interested friends can use 
Online HTML / CSS / JavaScript Front End Code Debugging Run Tool : http: // Tests the code running effect.

More about JavaScript Related Contents can also view this topic: “JavaScript Object-Oriented Telings”, “JavaScript Error and Debugging Skill Summary”, “JavaScript Data Structure and Algorithm Skills Summary”, “JavaScript Travel Algorithm Summary and “JavaScript Mathematical Computing Summary”

I hope this article will help you JavaScript programming.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment