Home >Web Front-end >JS Tutorial >What is JavaScript command pattern? Detailed explanation of command mode usage examples
Command mode concept
The definition of command mode (Command) is: used to parameterize and transmit method calls. The method calls processed in this way can be used wherever needed. time to execute. 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.
The functions and precautions of command mode
Mode functions:
1. Combining encapsulation, request and call into one
2. Call specific functions to decouple the command object and receiving object
3. Improve the flexibility of program modularization
Notes:
1. There is no need to use the same excuse, just call the function directly to avoid waste
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 detailed content of What is JavaScript command pattern? Detailed explanation of command mode usage examples. For more information, please follow other related articles on the PHP Chinese website!