JavaScript inheritance (temporarily omitting brokered functions, combined inheritance, and parasitic inheritance)

Source: Internet
Author: User

Inheritance, in JS through the prototype chain implementation. Such as:

function box () {    this. Name= "Lee";}  function Desk () {    this. age=100;}  // inheriting from the prototype chain, the object of the superclass instantiation is assigned to the prototype of the subclass desk.prototype=New  Box (); var desk=New  desk (); alert (desk.name);

In this way, desk's prototype gets the instance object + object information of box and forms the prototype chain.

Inheritance follows the nearest principle, and subclasses inherit the attributes closest to the parent class. Such as:

function box () {    this. Name= "Lee";}  // Add prototype property name Box.prototype.name= "Jack"; function Desk () {    this.age=100;} Desk.prototype=new  Box (); var desk=New  desk (); alert (desk.name);    If there is one in the instance, then go back to the prototype chain.

Subtype from which it belongs to itself or its super-type
Alert (desk instanceof desk);

Alert (desk instanceof Box);

On the use of the combination of object impersonation and prototype chain inheritance, to solve the construction method of communication and prototype sharing problems, later to further study.

JavaScript inheritance (temporarily omitting brokered functions, combined inheritance, and parasitic inheritance)

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.