JS Object-oriented Inheritance implementation

Source: Internet
Author: User
The implementation principle of JS Object-oriented Inheritance is as follows: 1. use the call () method (Object impersonating Mechanism) to inherit the attributes of the parent class from call ([thisObj [, arg1 [, arg2 [,[,. argN]) & amp; nbsp; call one of an object

The implementation principle of JS Object-oriented Inheritance is as follows:
1. Use the call () method (Object impersonating Mechanism) to inherit the attributes of the parent class
Call ([thisObj [, arg1 [, arg2 [, [,. argN]) call a method of an object to replace the current object with another object.
2. inherit the parent class method through the prototype attribute (prevent duplicate copies of the method from being created, resulting in a waste of resources)
Example:

/*** Js Object-oriented Inheritance ** @ link http://www.phpddt.com * // The parent class function Person (name, age) {this. name = name; this. age = age;} Person. prototype. showName = function () {alert (this. name);} Person. prototype. showAge = function () {alert (this. age);} // subclass function Student (name, age, grade) {Person. call (this, name, age); this. grade = grade;} for (var I in Person. prototype) {Student. prototype [I] = Person. prototype [I]; // inherits the members of the prototype chain} // The new member Method Student. prototype. showGrade = function () {alert (this. grade);} var p1 = new Person ("James", "20"); p1.showName (); var p2 = new Student ("Xiaohong", "21 ", "80 points"); p2.showName (); p2.showGrade ();
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.