JavaScript Design Patterns and development practices---reading notes (1)

Source: Internet
Author: User

PrefaceThe design pattern is defined as a simple and elegant solution to a particular problem in the object-oriented software design process. From some point of view, the design pattern does have the potential to increase the amount of code, perhaps making the logic of the system more Complex. But the cost of software development is not all in the development phase, the role of design mode is to let people write a reusable and maintainable high Program. The implementation of all design patterns follows a principle of "identifying changes in the program and encapsulating them." The invariant and stable parts are very easy to reuse. The key to the resolution pattern is that the intention, not the structural pattern, is meaningful only in the context of the specific environment, and the key to the pattern is the scenario in which it occurs, and the problems that are solved for us.Basic KnowledgeJavaScript implements inheritance between objects and objects by means of a prototype Delegate. JavaScript is a typical dynamic type Language. The tolerance of dynamic type language to variable type gives a lot of flexibility to the actual coding. Duck types instruct us to focus on the behavior of the object, not on the object itself, but on has-a, not Is-a. A principle implemented in a dynamic type language: "interface-oriented programming, not implementation-oriented programming". "interface-oriented programming" is the most important idea in design patterns, but in the JavaScript language, the process of "interface-oriented programming" is different from the mainstream static language. polymorphicThe actual meaning of polymorphism is that the same operation acts on different objects and can produce different interpretations and different execution results. In other words, sending the same message to different objects gives different feedback based on the Message. The idea behind polymorphism is to separate "what to do" and "who to do and how to do", that is, to separate "unchanging things" from "things that may change". By isolating the immutable parts and encapsulating the mutable parts, which gives us the ability to extend the program, the program seems to be growing, and is also in line with the open-closed principle, which is obviously more elegant and more secure than just adding code to the code to do the same thing. It is the most common means for the object to show polymorphism by polymorphism type checking and polymorphism using inheritance to get polymorphic effect. Inheritance typically includes implementation inheritance and interface Inheritance. The idea of polymorphism is to separate the "what" and "who", and to achieve this, the first is to eliminate the coupling between types. The most fundamental function of polymorphism is to eliminate these conditional branching statements by translating the conditional branching statements into the polymorphism of the Objects. The advantage of object-oriented design is that the behavior is distributed across objects and the objects are responsible for their respective behaviors.PackageThe purpose of encapsulation is to hide Information. In javascript, we can only rely on the scope of variables to achieve the sealing characteristics, and can only simulate the public and private of the two Encapsulation. In addition to the let of ES6, we typically create scopes through functions. Marshaling is more than just hiding data, it also includes hiding implementation details, design details, and types of hidden Objects. Objects communicate only through exposed API Interfaces.prototype ModePrototype mode is not just a design pattern, it is also called a programming generic. Using cloned prototype mode if you use prototype mode, we only need to invoke the method responsible for cloning, we can do the same function. ES5 provides a object.create method that you can use to clone Objects. Cloning is a means of creating objects that provides a convenient way to create objects of a type, and cloning is simply the process and means of creating this Object. Its object system is built using prototype patterns, which may be more appropriate to call prototype programming Paradigms. JavaScript is the use of prototype mode to build the entire object-oriented System. The delegation mechanism based on prototype chain is the essence of prototype Inheritance. The prototype programming paradigm includes at least the following basic principles:
    • All the data are Objects.
    • To get an object, instead of instantiating the class, find an object as a prototype and clone it.
    • The object remembers its Prototype.
    • If the object cannot respond to a request, it delegates the request to its own prototype.

Prototype inheritance in JavaScript

1. All the data are objects

JavaScript designers are meant to be objects, except Undefined.

These objects are traced from this root object.

The root object in JavaScript is the Object.prototype object. The Object.prototype object is an empty Object. Every object that we encounter in JavaScript is actually cloned from the Object.pototype object, and the Object.prototype object is their prototype.

2. To get an object, instead of instantiating the class, find an object as a prototype and clone it

JavaScript functions can be called either as normal functions or as Constructors. When you use the new operator to invoke a function, the function at this point is a constructor.

3. The object will remember its prototype

JavaScript provides an object with a hidden property named _proto_, and the _proto_ property of an object defaults to its Constructor's prototype object, which is {constructor}.prototype

_proto_ is the link between the object and the prototype of the object Constructor.

4. If the object cannot respond to the request, it will delegate the request to its Constructor's prototype

The prototype of the object constructor is not limited to object.prototype, but can point to other objects Dynamically.

Prototype mode is a design pattern and a programming generic, which forms the root of the JavaScript language.

JavaScript Design Patterns and development practices---reading notes (1)

Related Article

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.