JavaScript design pattern-appearance pattern _ javascript skills

Source: Internet
Author: User
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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.