functionDefineSubclass (Superclass,//the constructor of the parent classConstructor//the constructor for the new child classMethods//instance method: Copying to the prototypeStatics//class properties: Copying into constructors{ //creating a prototype object for a subclassConstructor.prototype =inherit (Superclass.prototype); Constructor.prototype.constructor=constructor; //copy methods and class properties as you would for regular classes if(methods) extend (constructor.prototype,methods); if(statics) extend (constructor,statics); returnConstructor;}functioninherit (p) {if(!p) {ThrowTypeError ();} if(object.create) {object.create (P);} vart =typeofp; if(t!== "Object" && t!== "function") { ThrowTypeError (); } functionf () {}; F.prototype=p; return Newf ();}functionExtend (proto,methods) {Proto[methods]=methods (); returnProto;}functionPerson (name,age) { This. Name =name; This. Age =Age ;} Person.prototype.eat=function() {alert ("eat!"));}functionStudent () {}definesubclass (person,student,functionLearn () {alert ("learn!");})
This is done by means of the parent class constructor
function (constructor,methods,statics) { return definesubclass (This, constructor, methods,statics);}
JavaScript Exercise-Defining subclasses