The first thing to say is that JavaScript can actually be programmed for OOP, and then there are many ways to implement OOP programming in JavaScript, which I've written that I've tested and that is feasible.
Version1
//Parent ClassfunctionPerson (name) { This. Name =name} Person.prototype.say=Saymyname;functionSaymyname () {alert ( This. Name)}//sub-classfunctionEmployee (name) {Person.call ( This, name)}//calling the parent class constructorEmployee.prototype =NewPerson ();//Inherit parent classEmployee.prototype.cardID =function() {alert ("1000")}//define your own methods for subclasses//instantiation ofp =NewEmployee ("Lishujun");p. Say ();p. CardID ();
Version2
//Parent ClassfunctionPerson (name) { This. Name =name} Person.prototype.say=Saymyname;functionSaymyname () {alert ( This. Name)}//sub-classfunctionEmployee (name) {Person.call ( This, name)}//calling the parent class constructorEmployee.prototype =NewPerson ();//Inherit parent classEmployee.prototype.cardID =function(){ This. Say (); Alert ("1000")}//define the subclass's own method, call the parent class method//instantiation ofp =NewEmployee ("Lishujun");p. Say ();p. CardID ();
Version3
//Parent ClassfunctionPerson (name) { This. Name =name} Person.prototype.say=Saymyname;functionSaymyname () {alert ( This. Name)}//sub-classfunctionEmployee (name) {Person.call ( This, name)}//calling the parent class constructorEmployee.prototype =NewPerson ();//Inherit parent classEmployee.prototype.cardID =function() {alert ("1000")}//define your own methods for subclassesEmployee.prototype.say =function() {alert ("Employee say")}//overrides the parent class method, that is: polymorphic //instantiation ofp =NewEmployee ("Lishujun");p. Say ();p. CardID ();
OOP Programming for JavaScript 1