Function.prototype.addMethod = function(){ if(arguments.length < 1) return; var tostring = Object.prototype.toString; if(tostring.call(arguments[0]) === '[object Object]'){ for(var key in arguments[0]){ this.prototype[key] = arguments[0][key]; } }else if(typeof arguments[0] === "string" && tostring.call(arguments[1]) === '[object Function]'){ this.prototype[arguments[0]] = arguments[1]; } return this; }
类式添加追求的就是个性化
Function.prototype.addMethod = function(){ if(arguments.length < 1) return; var cout = 0, tostring = Object.prototype.toString, that; if(typeof arguments[0] === "boolean" && arguments[0]){ cout++; that = this; }else{ that = this.prototype; } if(tostring.call(arguments[cout]) === '[object Object]'){ for(var key in arguments[cout]){ that[key] = arguments[cout][key]; } }else if(typeof arguments[cout] === "string" && tostring.call(arguments[cout + 1]) === '[object Function]'){ that[arguments[cout]] = arguments[cout + 1]; } return this; } //text var Text1 = function(){}; Text1 .addMethod('sayHello', function(){console.log('last say hello!')}) .addMethod('leave', function(){console.log('last goodbye!')}); var t = new Text1(); t.sayHello(); t.leave(); var test2 = function(){}; test2 .addMethod(true, 'sayHello', function(){console.log('last say hello!')}) .addMethod(true, 'leave', function(){console.log('last goodbye!')}); test2.sayHello(); test2.leave();
追求个性化,这么做不必说为什么