The connection between explicit prototypes and implicit prototypes in JavaScript

Source: Internet
Author: User

An explicit prototype: prototype

Implicit prototype: __proto__

1. What is an explicit prototype and an implicit prototype?

In JS everything is Object, method (Function) is an object, the prototype of the method (Function.prototype) is an object, the object has an attribute (__proto__) is called an implicit prototype, the object's implicit prototype points to the explicit prototype of the constructor that constructs the object.

A method (Function) is a special object that, in addition to having the __proto__ property as other objects, has a unique prototype property (prototype), which is a pointer to the prototype object. The prototype object also has a property called constructor, which contains a pointer to the original constructor.

Note: Functions constructed by the Function.prototype.bind method do not have a prototype property.

Note: Object.prototype. This object is an exception, and its __proto__ value is null.

2. The relationship between the two

The implicit prototype points to the prototype of the function that created the object

Let's start by looking at how to create an object

A. By the way the object literal.

var person={    name:"Tom"}

B. Create by means of new

// Create a constructor function function Person (name) {    this.name=name}// Create an instance of a constructor var person1=new person;

C. Create through Object.creat () mode

But in essence, 3 methods are created by means of new.

One of the objects created by Object.creat (O) is his implicit prototype pointing to O.

Objects created by object literals His implicit prototype points to object.prototype.

constructor function The person is essentially created by the function constructor, which is an instance of the function. The prototype object is essentially created by the object constructor. The built-in function, array number, is also created with a function constructor.

Therefore, it is not difficult to understand the following examples:

// by means of new // true // true // true // built-in functions // true // true

The __proto__ of function points to the prototype of its constructor function;

Object as a constructor (is a function Object!!) Function Object!), so his __proto__ points to Function.prototype;

The __proto__ of Function.prototype points to the prototype of its constructor object;

The __prototype__ of Object.prototype points to null (end);

The connection between explicit prototypes and implicit prototypes in JavaScript

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.