Home >Web Front-end >JS Tutorial >JavaScript design pattern classic command pattern_javascript skills

JavaScript design pattern classic command pattern_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:13:481841browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn