This article mainly introduces the appearance mode example of the JavaScript design mode. This article uses some code examples to explain the appearance mode in JavaScript. If you need a friend, refer to the appearance mode (appearance mode ), is a relatively simple and ubiquitous mode. The appearance mode provides a high-level interface that makes it easier for clients or subsystems to call.
It is represented by a piece of simple code:
The Code is as follows:
Var getName = function (){
Return "svenzeng"
}
Var getSex = function (){
Return 'man'
}
If you need to call the getName and getSex functions respectively, you can use a higher-level interface getUserInfo to call.
The Code is as follows:
Var getUserInfo = function (){
Var info = a () + B ();
Return info;
}
The answer is obvious. The cooking master in the dining hall won't cook these two dishes in one pot because you have made a duck and a cabbage order. He is more willing to provide you with a roast duck meal package. Similarly, in programming, we need to ensure that the function or object is at a reasonable granularity as much as possible.
Another advantage of the appearance mode is that the real implementation details can be hidden from the user, and the user only cares about the interface at the highest level. For example, in the story of the roast duck meal package, you do not care whether the master is roast duck or fried cabbage, or where the duck grows.
Finally, let's write an example of the appearance mode we have used:
The Code is as follows:
Var stopEvent = function (e) {// both stop the default event behavior and bubble
E. stopPropagation ();
E. preventDefault ();
}