Javascript inheritance-prototype inheritance (null function and cyclic copy) (3)

Source: Internet
Author: User

I. Using the null function to implement inheritance refer to the article on javascript inheritance-prototype attribute introduction (2) Comment on ye xiaochai, and modify solution 2 in this article using an empty function, this solution solves the resource consumption issues of special ownership and privilege methods, private attributes, and private methods during parent class instantiation when creating subclass objects. Copy the code function Person (name, age) {this. name = name; this. age = age;} Person. prototype = {constructor: Person, sayHi: function () {alert ('Hi') ;}} function Student (name, age, grade) {Person. call (this, name, age); this. grade = grade;} function Empty () {} Empty. prototype = Person. prototype; Student. prototype = new Empty (); Student. prototype. constructor = Student; var p1 = new Person ('xiaoming', 10); var s1 = new St Udent ('xiaohong ', 9, 3); console. log (p1); // Person {name = "xiaoming", age = 10, sayHi = function ()} console. log (s1); // Student {name = "xiaohong", age = 9, grade = 3, more ...} console. log (p1.constructor); // Person (name, age) the instance of the parent class still points to the parent class console. log (s1.constructor); // Student (name, age, grade) // The subclass instance points to the prototype object of the Person without modifying the prototype object of the Student if it is still a subclass copy code. In addition, the prototype of the Person is directly assigned to the prototype of Empty, therefore, there is no question about resource waste due to privileged attributes (instance attributes ). Question. In this way, the inheritance of common methods can be well solved by using NULL functions. Of course, the constructor in Student. prototype is Person, so it is best to add Student. prototype. constructor = Student to convert it. Ii. Implement inheritance by means of cyclic traversal and copy. Similarly, solution 3 of Article javascript inheritance-prototype attribute introduction (2) is actually a copy method, copy all common methods of the parent class to the subclass. Copy the code function Person (name, age) {this. name = name; this. age = age;} Person. prototype = {constructor: Person, sayHi: function () {alert ('Hi') ;}} 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]} Student. prototype. constructor = Student; Student. prototype. study = function () {alert ('Study ');} var p1 = New Person ('xiaoming', 10); var s1 = new Student ('xiaohong ', 9, 3); console. log (p1); // Person {name = "xiaoming", age = 10, sayHi = function ()} console. log (s1); // Student {name = "xiaohong", age = 9, grade = 3, more ...} console. log (p1.constructor); // Person (name, age) the instance of the parent class still points to the parent class console. log (s1.constructor); // Student (name, age, grade) // The subclass instance points to the still subclass copy code. This method directly copies the common methods of the parent class to the subclass using the traversal mode. This avoids the problem that subclass instances direct to the parent class, and does not affect the parent class by modifying the common methods of the subclass. It is also a perfect inheritance. Of course, the inheritance implementation of common methods in js is discussed here. The call method (which can also be considered as binding the constructor of the parent class to the subclass) is mainly used for property inheritance.

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.