Another talk on JavaScript prototype inheritance

Source: Internet
Author: User
Tags constructor inheritance

In the real sense, JavaScript is not an object-oriented language, does not provide a traditional way of inheritance, but it provides a way of prototype inheritance, using the prototype properties provided by itself to achieve inheritance.

Prototype and prototype chain

Before inheriting the prototype, we must first talk about the prototype and the prototype chain, after all, this is the basis for implementing the prototype inheritance.
In JavaScript, each function has a prototype attribute prototype to its own prototype, and the object created by the function also has a __proto__ attribute pointing to the prototype, and the function's prototype is an object, so the object also has a __proto__ Point to its own prototype, so that it is layered into the object's prototype, thus forming the prototype chain. The following diagram is a good explanation of the relationship between the prototype and the prototype chain in JavaScript.

Each function is an object created by the function functions, so each function also has a __proto__ property that points to the function's prototype. It is important to note here that the __proto__ property of each object, rather than the prototype attribute of the function, is the real form of the prototype chain.

Prototype inheritance

Basic mode

The

code is as follows:


var Parent = function () {


this.name = ' parent ';


} ;


Parent.prototype.getName = function () {


return this.name;


} ;


Parent.prototype.obj = {a:1};

var child = function () {
this.name = ' child ';
} ;
Child.prototype = new Parent ();

var parent = new parent ();
var child = new Child ();

Console.log (Parent.getname ()); Parent
Console.log (Child.getname ()); Child

This is the simplest way to implement the prototype inheritance, directly assigning the object of the parent class to the prototype of the subclass constructor, such that the object of the class can access the properties in the parent class and the prototype of the parent class constructor. The prototype inheritance diagram for this method is as follows:

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.