Command is defined as a parameter processing and transmission of method calls. a method call that has been processed in this way can be executed as needed. Next, we will use this article to introduce you to the typical JavaScript design mode command mode. For more information, see
I. concept of command mode
Command is defined as a parameter processing and transmission of method calls. a method call that has been processed in this way can be executed as needed. That is to say, this mode aims to encapsulate function calls, requests, and operations into a single object, and then process this object with some columns. It can also be used to eliminate coupling between objects that call operations and objects that implement operations. This brings great flexibility for the replacement of various specific classes.
II. Functions and precautions of the command mode
Role of the mode:
1. integrate function encapsulation, requests, and calls
2. call specific function decoupling command objects and receiving objects
3. improve the flexibility of program modularization
Note:
1. you can directly call the function without excuses for consistency to avoid waste.
III. command mode code and practical summary
Script // 1. the commander can run commands to mobilize the army to fight with artillery and infantry. var lian ={}; lian. paobing = function (pao_num) {console. log (pao_num + "warehouse guard prepare for battle");} lian. bubing = function (bubing_num) {console. log (bubing_num + "prepare for battle");} lian. lianzhang = function (mingling) {lian [mingling. type] (mingling. num);} // command lian. lianzhang ({type: "paobing", num: 10}); lian. lianzhang ({type: "bubing", num: 100}); script
The above section describes the typical JavaScript design mode of the command mode. I hope it will be helpful to you!