JavaScript prototype Pattern Summary grooming

Source: Internet
Author: User

In most object-oriented languages , objects are always instantiated from a class , and the relationship between classes and objects is like a stencil. There is no concept of class in JavaScript, even if the class introduced in ES6 is just a syntactic sugar, essentially using prototypes. In a prototype programming language, a class is not required, and an object does not necessarily need to be instantiated by a class, but rather by cloning another object.

Prototype mode is a pattern used to create objects. In a class-centric language, to create an object, you first specify the type of the object, and then instantiate an object. When you create an object using prototype mode, you do not have to care about the object's specific type, but instead find an object and then clone it to create an identical object. So in the former, if we want to create multiple identical objects based on an object, we need to save all the property information of the object, then set the property information to the newly created object, and in prototype mode we just need to use the clone to accomplish the same function.

In some fantasy novels, some of them often appear to be able to do some real work and walk the world in the form of a fen. This process is well suited for prototype mode applications:

function Master () {    this. Blood =;      this. level = 6;} var New  = 9; var ektype = object.create (noumenon); Console.log (ektype);

ES5 provides native cloning methods: object.create, browsers that do not support this method can use the following code:

function Clone (obj) {    function  F () {};     = obj;     return New F ();} var ektype = Clone (Noumenon);

With the above code, we see How to clone an identical object using the prototype pattern. The true meaning of the prototype pattern is not to create an identical object, but rather to provide a way to create the object, the object-oriented mechanism of JavaScript is based on the prototype pattern, his object system is to use the prototype pattern, through cloning, cloning is the process and means of creating an object. Take inheritance as an example:

function Person (name) {    this. Name = name;}  function Developer (lang) {    this. Language = lang;}  var New Person (' coder '= p; var New Developer (' Javascript ');

Based on the prototype inheritance system, each instantiation of a subclass is a clone of the prototype property of its constructor. So every time you create a developer object, it's actually a clone of the P object.

In class-centric object-oriented languages such as Java, you often instantiate an object with new. But JavaScript is a prototype-based object-oriented language, where the new operator creates objects differently from the new operator in Java, and thenew operator in JavaScript is also a clone of the object, and the cloned constructor function is the prototype object , the new operator is equivalent to the following code:

functionPerson (name) { This. Name =name;}functionDeveloper (lang) { This. Language =Lang;}varp =NewPerson (' coder ');D Eveloper.prototype=p;function_new (_constructor) {varthat =object.create (_constructor.prototype); varargs = Array.prototype.slice.call (arguments, 1); varother =_constructor.apply (that, args); return(typeofother = = = ' object ' && other)?Other:that;} _new (Developer,' JavaScript ')

From this we can also see that the JavaScript prototype actually has a lot of contradictions, some of its complex syntax looks like those based on the language of the class, which masks its prototype mechanism. So try to avoid creating objects with the new operator in jquery.

The newly created object in JavaScript is based on the clone of the original object, so there is one primitive object in javascript: Object.prototype, all objects are cloned from it.

  The cloning described here is a semantic expression in the context of JavaScript prototype mode, where there is no real cloning in the physical world of computers. So here for cloning it should be understood as the process of producing an object that has the __proto__ attribute pointing to the original object, which becomes the cloned object, the prototype object of the constructor.

With the above consensus, we can get the basic rules for programming in javascript:

    1. Most of the data in JavaScript is an object
    2. To get an object, instead of instantiating the class, find an object as a prototype and clone it
    3. The object remembers its prototype.
    4. If the object cannot respond to a request, he will delegate the request to its own prototype.

Reference books:

The essence of JavaScript language

JavaScript Design patterns and development practices

JavaScript prototype Pattern Summary grooming

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.