JS's prototype chain

Source: Internet
Author: User

JS in the prototype chain is a very important concept, understand the prototype chain, the development of JS program has great benefits, nonsense does not say, first:

JavaScript is a prototype-based language, so an object can inherit from another object. However, JavaScript is a bit of a hassle when it comes to implementation, and it does not directly allow an object to inherit from another object. But to imitate other classes-based languages,

is also a build constructor. Except that JavaScript does not show a declarative function, and every function of JavaScript can be a constructor. When a function is called, an instance is created as long as it is preceded by the new operator, which is actually

An object. This object has an intrinsic property __proto__ pointing to a prototype object. When we run the program to access the properties or methods of our new object, we will first look for the property or method in our newly created object, and if found

Returned, if not found, the object's __proto__ property is used to find its prototype object with no properties or methods that we need.

So what exactly is the __proto__ of the object that we're declaring is pointing to that prototype object? Let's take a look at a combination of examples.

Take the above constructor, a for example, when function A is created, a is accompanied by a property that generates a prototype, and the object to which the attribute points is represented by the code: A.prototype = {constructor:a}.

The __proto__ property of New A () points to a.prototype. So what happens when I call the new operator? "The Best of JavaScript language" is very good in the book, want to see the details of the book, please step into the fifth chapter Ah!

If you look at the new operator as a method, it may be executed as follows:

A, you will first create a new object and point the __proto__ property of the new object to A.prototype.

B, call constructor A, bind this to our newly created object.

C, after the judgment function a call there is no return value of the object type, if any, return the object, if not, return the new object we created.

So, although JavaScript is a prototype-based language, it does not directly allow objects to inherit from other objects, but it is a step in the process of generating objects from the constructor and inheriting other objects.

Through, there are some points that we need to be aware of:

1. In JavaScript, a function is an object, except that it has two more hidden properties than other ordinary objects: The function context and the invocation property of the calling function code. In constructor A is an instance of function,

Likewise object is.

2, examples, constructors, prototype objects of the relationship between the three should be noted. There is no direct relationship between the instance and the constructor.

3, through the __proto__ property of the object when looking for properties, if not found, will continue to look up until the root object object.prototype.

4, Function.prototype = = = function.__proto__; A function is its own constructor. That is: function.constructor = = = Function.

Finally leave a question, combine, object instanceof object;  function instanceof function; Object instanceof Function; Function instanceof The value of Object, respectively? Why

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.