Basic concepts of JavaScript-based objects

Source: Internet
Author: User

Before we learned about objects, we talked about how to create objects, one that creates a new object through a constructor, and one that creates objects in the form of object literals.

It is important to understand the concept of a prototype object when it comes to objects.

1 concept of prototype objects

The

constructor has a default property, prototype property, which points to an object that is the prototype object of the constructor.

The

also has a constructor attribute on the prototype object, which points to the constructor itself.

2 features in a prototype object

The properties and methods in the

Prototype object can be shared by Instance objects (the properties and methods in the prototype function called by different instance objects are the same).

3 instance object points to prototype object

The instance object created by the constructor has a __proto__ property that points to the prototype object of the constructor.

However, because the __proto__ property is nonstandard, it is not recommended to use the __proto__ property to find the prototype function. If you need to set properties on a prototype object, we recommend that you use the prototype property of the constructor to locate the prototype object.

4 instance calls the property or method's procedure

When the instance. Property name is called, the instance first looks for the property in itself, and if it does not have this property, it finds the prototype object, finds it in the prototype object, and if it has not yet found the prototype object on the prototype, until the top of the prototype chain (that is, no longer have the prototype object).

5 Object Structure map

The

Prototype object is more abstract, and we can only deepen the understanding through the structure diagram.

Case code:

 function Animal (name,color) {
This.name=name;
This.color=color;
}
Animal.prototype.cry=function () {
Console.log ("I Am" +this.name, "I was bullied, 55555");
}
var cat=new Animal ("Tony", "gray-white");
var dog=new Animal ("Ha II", "Brown");
The corresponding structure diagram:

 

Basic concepts for JavaScript-based objects

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.