Javascript Internal principles [javascript implementation inheritance]

Source: Internet
Author: User

Javascript object-oriented is not a new transaction. Now it is simple to simulate OOP implementation inheritance.

/*** Inherit */(function () {// create a member class function Person (name) {this. name = name;} // create a Teacher class function Teacher (name, books) {// The call method can change the object context of a function from initialization to this. // call the Person constructor, because Person does not use new, it is an empty object // It is equivalent to the super function Person in java. call (this, name); this. books = books;}/*** create an extend function for all inherited operations */function extend (subClass, superClass) {// The subClass prototype class property is equal to the parent class prototype class property subClass. prototype = new superClass (); subClass. prototype. constructor = subClass; // for future convenience, that is to say, you can directly call subClass without knowing the parent class name. // we add the following statement. superClass = superClass. prototype;} function Author (name, books) {// Author. superClass. constructor. call (this, name); // inherit. assign a value to the name attribute in this class to inherit Author. superClass. constructor. call (this, name); // Person. call (this, name); this. books = books; this. getBook = function () {return this. name + "" + this. books ;}} extend (Author, Person); var peter = new Author ("123", "JAVASCIPT"); alert (peter. getBook ())})();


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.