JavaScript Design Patterns Learning Notes

Source: Internet
Author: User

1 JavaScript Design Patterns in-depth analysis

private Properties and methods: functions have scopes, variables declared with the VAR keyword within a function cannot be accessed externally, and private properties and methods are essentially variables that you want to be inaccessible outside of the object.

Privileged properties and methods: The This keyword is used when creating properties and methods, because these methods are defined in the scope of the constructor, so they can access private properties and methods; only those methods that require direct access to private members should be designed as privileged methods.

Common Properties and methods: properties and methods that chain directly on prototype, cannot access private members within the constructor, access privileged members, and subclasses inherit all common methods.

common static properties and methods: the best way to understand this is to think of it as a namespace, which is actually equivalent to using the constructor as a namespace.

/*--Package--*  /var _packaging = function () {      //private property and Method      var name = ' Darren ';      var method1 = function () {         //...      }      Privileged properties and methods      this.title = ' JavaScript Design Patterns ';      This.getname = function () {         return name;      }   }   Common static properties and methods   _packaging._name = ' Darren code ';   _packaging.alertname = function () {      alert (_packaging._name);   }  Common Properties and methods  _packaging.prototype = {      init:function () {         //...      }   

 The role of JavaScript design patterns-improves code reusability, readability, and makes code easier to maintain and extend.

1. Monomer mode, Factory mode, bridge mode personally, this is a good front-end must master the pattern of abstract programming and interface programming is very beneficial.

2. There are many similarities between the decorator pattern and the composition pattern, which implement the same interface as the wrapped object and pass the invocation of any method to those objects. Decorator mode and the combination of the model I described the more difficult two models, I personally have not used, so check a lot of relevant information and documentation, please everyone Haihan.

3. The façade mode is a very interesting pattern, almost all JavaScript libraries will use this mode, if you have the reverse thinking or reverse programming experience, you will be easier to understand this mode (it sounds challenging, in fact, a touch you know this is a very simple mode); There are configurator mode and façade mode a piece of it, this mode to the existing interface packaging, reasonable use can improve the development efficiency to a large extent. There are similarities between the two patterns, so a piece of understanding is believed to be quick to get started.

4. The enjoy meta mode is a model for optimization purposes.

5. The proxy mode is primarily used to control access to objects, including deferring the instantiation of classes that require a large amount of computational resources to be created.

6. The Observer pattern is used to observe the state of an object and to be notified when it is changed. Used to let an object listen to an event to respond to it. The Observer pattern is also known as the subscriber pattern.

7. The command pattern is the way the method calls are encapsulated, and the method invocation can be parameterized and passed by the named schema, and then executed when needed.

8. The responsibility chain pattern is used to eliminate the coupling between the sender and receiver of the request.

Monomer (Singleton) mode

The way to use monomers is to use a single namespace to contain all of your own code global objects

Factory (Factory) mode

Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Bridging (bridge) mode

Bridging mode is the function of separating the implementation part from the abstract part so that the two can change independently. Bridging mode is especially useful when implementing APIs

Observer pattern

Div.onclick  =  function Click () {   alert (' click ')}

  

2 Common JavaScript Design Patterns

3 Learning JavaScript Design Patterns

JavaScript Design Patterns Learning Notes

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.