Home  >  Article  >  Web Front-end  >  JavaScript Design Pattern Appearance Pattern Example_javascript skills

JavaScript Design Pattern Appearance Pattern Example_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:34:351262browse

Appearance mode (facade mode) is a relatively simple and ubiquitous mode. The appearance pattern provides a high-level interface that makes it easier for clients or subsystems to call.

Use a simple piece of code to express it:

Copy 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 separately, you can use a higher-level interface getUserInfo to call it.

Copy code The code is as follows:

var getUserInfo = function(){
var info = a() b();
return info;
}

The answer is obvious. The stir-fry chef in the canteen will not stir-fry the two dishes in the same pot just because you ordered a portion of roast duck and a portion of cabbage. He would rather offer you a roast duck rice set. Also in programming, we need to ensure that functions or objects are at a reasonable granularity as much as possible. After all, not everyone likes to eat roast duck and also likes to eat cabbage.

Another benefit of the appearance mode is that it can hide the real implementation details from users, who only care about the highest-level interface. For example, in the story of the roasted duck rice set, you don't care whether the chef cooks the roasted duck first or the cabbage first, and you don't care where the duck was grown.

Finally, let’s write an example of appearance mode that we have all used:

Copy code The code is as follows:

var stopEvent = function( e ){ //Stop event default behavior and bubbling at the same time
e.stopPropagation();
e.preventDefault();
}
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