I wrote it 1 months ago, and I wrote it again.
//class implementation, a parameter creation class, two parameter inheritance classesvarKlass =function(parent, options) {varHasown=Object.prototype.hasOwnProperty,//Intermediate function avoids executing parent class constructorFunc =function () { }, //mixin Shallow CopyExtend =function(receiving, giving) {varK; for(kinchgiving) { if(Hasown.call (giving, k)) {Receiving[k]=Giving[k]; } } }, //the returned subclassChild =function(opt) {Extend ( This, opt); }; //parameter Check if(Arguments.length = = 0 | | arguments.length > 2)Throw NewError ("Accept only 1 or 2 parameters"); //pass in only one parameter object if(typeofParent!== ' function ') {Options=parent; Parent=NULL; } //Inherit parent class if(parent) {Func.prototype=Parent.prototype; Child.prototype=NewFunc (); Child.prototype.constructor=Child ; } //Add Optionsextend (child.prototype, options); returnChild ;};//Testvarperson =Klass ({getName:function () { return This. Name; }, say:function() {Console.log ("Hello World"); }, Fav:' Js '}), Player=Klass (person, {play:function() {Console.log ("I ' m playing" + This. Game); }}), the player=NewPlayer ({name:' Bobo ', Game:' Dota2 '});p Layer.play (); //"I ' m playing Dota2"Player.say ();//"Hello World"Console.log (Player.getname () + "" + player.fav);//"Bobo Js"
The realization of JS class