The concept of prototypes

Source: Internet
Author: User

Prototype-related concepts
  1. About object-oriented concepts
    • Class: In JS is the constructor function
      • In the traditional object-oriented language, a template is defined using something called a class. Then use the template to create the object.
      • Similar functionality is also in the construction method. It is therefore called the class
        // 在 java 中, 最小的代码单位是 类class Program {// 成员}
    • Instances (instance) and objects (object)
      • An instance typically refers to an object created by a constructor. We become an example of XXX constructors
      • An instance is an object. The object is a general term.
      • Instances and objects are a synonym
    • Key-value pairs and properties and methods
      • The set of key-value pairs in JS is called an object
      • If the value is data (not a function), the key-value pair is called the property
      • If the value is a function (method), the key value pair is called the method
    • Parent class and Child class
      • The traditional object-oriented language uses classes to implement inheritance. Then there is the concept of a parent class, a subclass
      • The parent class is also called the base class, and the subclass is also called the derived class
      • In JS, it is often called the parent object, the child object. Base object, derived object.
  2. Prototype-related concepts
    • A mysterious object is called a "prototype attribute" for a constructor function.
      • The mysterious object is the prototype property of the constructor.
      • Abbreviation prototype
    • The mystery object is also related to the object created by the constructor.
      • What's the relationship?
      • The object created by the mysterious object for the constructor is called the prototype object.
      • Abbreviation prototype
    • Object inherits from its prototype
      • constructor-created object inherits the prototype property from the constructor
      • The object that the constructor creates inherits from the prototype object of the object
      • The object that the constructor creates for the object that is represented by the constructor's prototype property is two different objects
        • Members in the prototype that can be used directly by the instance object
        • That is, the instance object directly "contains" the members of the prototype.
        • So the instance object inherits from the prototype
        • Such inheritance is "Prototype inheritance"
  3. Some questions
    • What is the {} constructor?
    • Every literal object has a constructor.
      • {} Object
      • [] Array
      • /./REGEXP
      • Function ... Function
How to use Prototypes

Why use prototypes?

To improve the reusability of code

Attributes are not placed on the prototype: properties represent features that are easily changed

method is placed on the prototype: The method is treated the same way, can be reused

    1. Using the dynamic properties of objects
      • constructor. prototype.xxx = VVVV;
    2. Use Direct replacement
       Student.prototype = { sayHello: function () {}, study: function () {} };

The concept of prototypes

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.