Chat JavaScript inheritance and prototyping

Source: Internet
Author: User

JavaScript inheritance is a bad topic, I'll just talk about it.

First, the replication inheritance of JavaScript

The inheritance of JavaScript has replication inheritance and prototype inheritance, not too much for replication inheritance, and cannot be verified by instanceof

// copy inheritance, Prototype.js's extend=> function Extend (destination,source) {    for (var in source)    destination[ Property]=Source[properyt];     return destination;}

Second, JavaScript prototype inheritance

JS prototype inheritance is based on the prototype chain lookup, JS each function has the prototype property and the __proto__ property, each instance of the __proto__ property points to the prototype of the function (Es6 in the instance of __proto__ point to this function), The following example proves this point.

 function   A () {Console.log ( this . __proto__.aa); // 1  }a.prototype  ={aa:  1}  var  a=new   A;console.log (A.AA);  // 2  a.__proto__={aa:  3}  delete  A.aa; //  Console.log (A.AA); // 3  

The instance finds the method by the prototype chain, finds its own property, does not go to the prototype of the constructor function, and finds it in the prototype of the constructor function, only to the prototype of function. Then we let A's prototype equal to an instance of a, do not complete the inheritance.

function A () {}a.prototype={    aa:1}function  Bridge () {};bridge.prototype= A.prototype; function B () {}b.prototype=new  Bridge (); B.prototype.constructor=b;

This inheritance is done with a bridge function, because when a has a lot of content, the instantiation of a consumes more, and there is no use, just use an empty function to do the bridge. Here we go. The constructor of the instance points to itself, which completes the inheritance.

var b=New  B; b.prototype.cc=function() {    alert (3)}console.log (b.__proto__==b.prototype);   trueconsole.log (B.__proto__.__proto__===a.prototype); // true

The __proto__ of B here is the prototype that points to the constructor function.

Chat JavaScript inheritance and prototyping

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.