JavaScript Design patterns and development practices "part One"

Source: Internet
Author: User

Read the JavaScript design patterns and development practice today and have long been eager to learn about design patterns.

The design pattern is defined as a simple and elegant solution to a particular problem in the object-oriented software design process.

In fact, usually in the work unconsciously in the use of certain design patterns, but we do not know just.

Dynamic type language and static type language
    • The static type language determines the type of the variable at compile time, and the variable type of the dynamic type language will have some type when the variable is given a value when it is run by the program.
    • The advantage of a statically typed language is that the type mismatch error can be found at compile time, and the editor can help us avoid some of the errors that may occur during the program's operation. And the program to do some optimization work to improve the speed of program execution.
    • The tolerance of dynamic type language to variable type gives a lot of flexibility to the actual coding. Because there is no need for type detection, we can try to invoke any method of any object without having to consider whether it was originally designed to own the method.
      Here is the concept of a duck type.

"If it walks like a duck, and it is a duck, it is a duck," he said. ”

Using the duck type idea, we can easily implement a principle in a dynamic type language without the help of a super type: "interface-oriented programming, not implementation-oriented programming."

Polymorphic

The most fundamental benefit of polymorphism is that you no longer have to ask the object "What type you are" and then invoke an object's behavior based on the resulting answer-you just call that behavior, and all the other polymorphic mechanisms will be arranged for you.

JavaScript does not perform type checking and is easy to implement polymorphism, as long as you have this behavior. The static type language needs to be designed to be able to be transformed upward: when assigning a value to a class variable, the type of the variable can either use the class itself, or it can use the superclass of the class.

Packaging

The purpose of encapsulation is to hide information, encapsulation should be considered "any form of encapsulation", that is, encapsulation is not only hidden data, but also the hidden implementation details, design details and types of hidden objects.

Prototype mode

In JavaScript, each object is cloned from the Object.prototype object, so that we can only get a single inheritance relationship, that is, each object inherits from the Object.prototype object, and the object system is obviously very limited.
But we can point to other objects dynamically. In this way, when object a needs to borrow the ability of object B, it can selectively point the prototype of object A's constructor to object B, thus achieving the effect of inheritance. The following code is the usual way to inherit the prototype chain:

var obj = {    ' Sven '}; var function  = obj; var New  //  Sven

Github:https://github.com/vxee/articles/issues/11

JavaScript Design patterns and development practices "part One"

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.