Home > Article > Web Front-end > JavaScript design pattern classic command pattern_javascript skills
1. Concept of command mode
The definition of command mode (Command) is: used to parameterize and transmit method calls. The method calls processed in this way can be executed whenever needed. That is to say, this pattern aims to encapsulate function calls, requests and operations into a single object, and then perform a series of processes on this object. It can also be used to decouple the object that calls the operation from the object that implements the operation. This brings great flexibility to the replacement of various specific classes.
2. The functions and precautions of command mode
Mode function:
1. Combine function encapsulation, request and call into one
2. Call specific functions to decouple the command object and the receiving object
3. Improve the flexibility of program modularization
Note:
1. There is no need to use the same excuse, just call the function directly to avoid waste
3. Command mode code and practical summary
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <script> //1.一个连有炮兵和步兵,司令可以下命令调动军队打仗 var lian = {}; lian.paobing = function(pao_num){ console.log(pao_num+"门炮准备战斗"); } lian.bubing = function(bubing_num){ console.log(bubing_num+"人准备战斗"); } lian.lianzhang = function(mingling){ lian[mingling.type](mingling.num); } //司令下命令 lian.lianzhang({ type:"paobing", num:10 }); lian.lianzhang({ type:"bubing", num:100 }); </script> </body> </html>
The above is the classic command pattern of JavaScript design patterns introduced by the editor. I hope it will be helpful to you!