Simple examples of javascript implementation inheritance-javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the information about simple examples of javascript implementation inheritance. If you need a friend, you can refer to the following as an object-oriented language, inheritance is naturally a major feature, the following is a very simple code example. It demonstrates the basic principle of inheritance. If you are interested or want to learn about this, you can refer to it and hope to help you.

// Inherit function Person (name, sex) {this. name = name; this. sex = sex;} Person. prototype. sayName = function () {alert (this. name);} Person. prototype. saySex = function () {alert (this. sex);} function Worker (name, sex, job) {// inherits the person class Person. call (this, name, sex) // here this refers to the instance of the Worker class, 'w' below, pass w to the Person constructor, in this case, W is disguised as this in the Person constructor. job = job;} // Worker. prototype = Person. prototype; // if this is the negative value prototype, the sayJob method of the subclass "Person" parent class will also have the sayJob method, because it is a reference transfer // if it is changed to the following method, the subclass will not affect the parent class: for (var I in Person. prototype) {Worker. prototype [I] = Person. prototype [I];} Worker. prototype. sayJob = function () {alert (this. job);} var p = new Person ('lisi', 'male'); // alert (p. sayJob); var w = new Worker ('hangsan', 'male', 'soy sauce '); w. sayName (); w. saySex (); w. sayJob ();

The above is all the content of this article. I hope you will like it.

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.