javascript创建函数的20种方式汇总
工作中常常会创建一个函数来解决一些需求问题,以下是个人在工作中总结出来的创建函数20种方式,你知道多少?
functionsayHello(){ console.log('hello'); } functionleave(){ console.log('goodbye'); } //test sayHello();
为完成需求,赶紧声明一个函数吧
varsayHello=function(){ console.log('hello'); } varleave=function(){ console.log('goodbye'); } //test leave();
有求必应,函数表达数来解决
varAction={ sayHello:function(){ console.log('hello'); }, leave:function(){ console.log('goodbye'); } } //test Action.sayHello();
创建一个方法对象类看起来更整洁
varAction=function(){}; Action.sayHello=function(){ console.log('hello'); } Action.leave=function(){ console.log('goodbye'); } //test Action.sayHello();
为单体添加属性方法,净化命名空间
varAction=function(){ return{ sayHello:function(){ console.log('hello'); }, leave:function(){ console.log('goodbye'); } } } ////test vara=Action(); a.leave();
返回新对象我们还有更多的事情可以做
varAction=function(){}; Action.prototype.sayHello=function(){ console.log('hello'); } Action.prototype.leave=function(){ console.log('goodbye'); } //test vara=newAction(); a.sayHello();
原型链指向防止创建多次
varAction=function(){}; Action.prototype={ sayHello:function(){ console.log('hello'); }, leave:function(){ console.log('goodbye'); } } //test vara=newAction(); a.leave();
对象赋给原型看上去更整洁
varAction=function(){ this.sayHello=function(){ console.log('hello'); } this.leave=function(){ console.log('goodbye'); } } //test vara=newAction(); a.leave();
别忘了还可以在类的内部添加属性
Function.prototype.sayHello=function(){ console.log('hello'); } Function.prototype.leave=function(){ console.log('leave'); } //test varf=function(){}; f.sayHello();
基类原型拓展,新的一片空间
Function.prototype.addMethod=function(name,fn){ this[name]=fn; } varmethods=function(){}; methods.addMethod('sayHello',function(){ console.log('hello'); }); methods.addMethod('leave',function(){ console.log('leave'); }); //test methods.sayHello();
通用定义方法函数使用更方便
Function.prototype.addMethod=function(name,fn){ this.prototype[name]=fn; } varMethods=function(){}; Methods.addMethod('sayHello',function(){ console.log('hello'); }); Methods.addMethod('leave',function(){ console.log('leave'); }); //test vara=newMethods(); a.leave();
原形赋值我们还可以用类操作
Function.prototype.addMethod=function(name,fn){ this[name]=fn; returnthis; } varmethods=function(){}; methods.addMethod('sayHello',function(){ console.log('hello'); }).addMethod('leave',function(){ console.log('leave'); }); //test methods.leave();
链式操作有何不可
Function.prototype.addMethod=function(name,fn){ this.prototype[name]=fn; returnthis; } varMethods=function(){}; Methods.addMethod('sayHello',function(){ console.log('hello'); }).addMethod('leave',function(){ console.log('leave'); }); //test vara=newMethods(); a.leave();
原型+链式=更进一步
Function.prototype.addMethod=function(obj){ for(varkeyinobj){ this[key]=obj[key]; } } varmethods=function(){}; methods.addMethod({ sayHello:function(){ console.log('hello'); }, leave:function(){ console.log('goodbye'); } }); //test methods.leave();
添加对象一次做得更多
Function.prototype.addMethod=function(obj){ for(varkeyinobj){ this.prototype[key]=obj[key]; } } varMethods=function(){}; Methods.addMethod({ sayHello:function(){ console.log('hello'); }, leave:function(){ console.log('goodbye'); } }); //test vara=newMethods(); a.leave();
原型有什么不可以
Function.prototype.addMethod=function(obj){ for(varkeyinobj){ this[key]=obj[key]; } returnthis; } varmethods=function(){}; methods.addMethod({ sayHello:function(){ console.log('hello'); } }).addMethod({ leave:function(){ console.log('goodbye'); } }); //test methods.leave();
函数式添加对象也可以链式操作
Function.prototype.addMethod=function(obj){ for(varkeyinobj){ this.prototype[key]=obj[key]; } returnthis; } varMethods=function(){}; Methods.addMethod({ sayHello:function(){ console.log('hello'); } }).addMethod({ leave:function(){ console.log('goodbye'); } }); //test vara=newMethods(); a.leave();
类的链式操作也可以做得更多
Function.prototype.addMethod=function(){ if(arguments.length<1) return; vartostring=Object.prototype.toString; if(tostring.call(arguments[0])==='[objectObject]'){ for(varkeyinarguments[0]){ this[key]=arguments[0][key]; } }elseif(typeofarguments[0]==="string"&&tostring.call(arguments[1])==='[objectFunction]'){ this[arguments[0]]=arguments[1]; } returnthis; }
函数添加封装一下
Function.prototype.addMethod=function(){ if(arguments.length<1) return; vartostring=Object.prototype.toString; if(tostring.call(arguments[0])==='[objectObject]'){ for(varkeyinarguments[0]){ this.prototype[key]=arguments[0][key]; } }elseif(typeofarguments[0]==="string"&&tostring.call(arguments[1])==='[objectFunction]'){ this.prototype[arguments[0]]=arguments[1]; } returnthis; }
类式添加追求的就是个性化
Function.prototype.addMethod=function(){ if(arguments.length<1) return; varcout=0, tostring=Object.prototype.toString, that; if(typeofarguments[0]==="boolean"&&arguments[0]){ cout++; that=this; }else{ that=this.prototype; } if(tostring.call(arguments[cout])==='[objectObject]'){ for(varkeyinarguments[cout]){ that[key]=arguments[cout][key]; } }elseif(typeofarguments[cout]==="string"&&tostring.call(arguments[cout+1])==='[objectFunction]'){ that[arguments[cout]]=arguments[cout+1]; } returnthis; } //text varText1=function(){}; Text1 .addMethod('sayHello',function(){console.log('lastsayhello!')}) .addMethod('leave',function(){console.log('lastgoodbye!')}); vart=newText1(); t.sayHello(); t.leave(); vartest2=function(){}; test2 .addMethod(true,'sayHello',function(){console.log('lastsayhello!')}) .addMethod(true,'leave',function(){console.log('lastgoodbye!')}); test2.sayHello(); test2.leave();
追求个性化,这么做不必说为什么
以上所述就是本文的全部内容了,希望大家能够喜欢。