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.