Class inheritance: the way to inherit from constructors (classes)
Class: JS is a class without a concept, a class that treats constructors in JS as examples
funcTIon Aaa () { // parent class this .name = 'å°æ˜Ž' ; } Aaa.prototype.showName = funcTIon () { alert( this .name ); }; funcTIon Bbb () { //subclass } Bbb.prototype = new Aaa(); //This is called class inheritance var b1= new Bbb(); b1.showName(); //å°æ˜Ž ert(b1.name); //å°æ˜Ž(also a layer search)The following figure clearly shows the above relationship
Question 1: But the above inheritance is problematic. If we don't believe, we will do an experiment. Continue to add code based on the above code
Alert( b1 .constructor ) ;pop up
funcTIon Aaa () { //Parent class this .name = 'å°æ˜Ž' ; }So you should add a sentence to correct the problem.
Bbb.prototype = new Aaa(); //This is called class inheritance Bbb.prototype constructor = Bbb ; //fix pointing problemQuestion 2: Make the above code slightly modified
function Aaa () {// parent this .name = [1, 2, 3];} Aaa.prototype.showName = function () {alert (this .name);}; function Bbb () {// subclass } Bbb.prototype = new Aaa(); Bbb.prototype.constructor = Bbb; //Fixed to the problem var b1 = new Bbb(); b1.name.push( 4 ); var b2 = new Bbb(); alert( B2.name ); //[1,2,3,4] Analysis, b1, b2 are different objects, but the above affect each other, so there is a problem. Because both find the name under new Aaa(), it is a thing, so it will affect each other.
Can be corrected like this, as shown below
Plastic Capillary Pressure Gauge
Plastic Capillary Pressure Gauge,Mini-Sized Capillary Manometer,D28 Plastic Tube Pressure Gauge,Pipe Manometer
ZHOUSHAN JIAERLING METER CO.,LTD , https://www.zsjrlmeter.com