工作上常常會建立一個函數來解決一些需求問題,以下是個人在工作中總結出來的創建函數20種方式,你知道多少?
function sayHello(){ console.log('hello'); } function leave(){ console.log('goodbye'); } //test sayHello();
為完成需求,趕緊宣告一個函數吧
var sayHello = function(){ console.log('hello'); } var leave = function(){ console.log('goodbye'); } //test leave();
有求必應,函數表達式來解
var Action = { sayHello : function(){ console.log('hello'); }, leave : function(){ console.log('goodbye'); } } //test Action.sayHello();
建立一個方法物件類別看起來比較整潔
var Action = function(){}; Action.sayHello = function(){ console.log('hello'); } Action.leave = function(){ console.log('goodbye'); } //test Action.sayHello();
為單體加入屬性方法,淨化命名空間
var Action = function(){ return { sayHello : function(){ console.log('hello'); }, leave : function(){ console.log('goodbye'); } } } // //test var a = Action(); a.leave();
回傳新物件我們還有更多的事情可以做
var Action = function(){}; Action.prototype.sayHello = function(){ console.log('hello'); } Action.prototype.leave = function(){ console.log('goodbye'); } //test var a = new Action(); a.sayHello();
原型鏈指向防止創建多次
var Action = function(){}; Action.prototype = { sayHello : function(){ console.log('hello'); }, leave : function(){ console.log('goodbye'); } } //test var a = new Action(); a.leave();
物件賦給原型看上去比較整潔
var Action = function(){ this.sayHello = function(){ console.log('hello'); } this.leave = function(){ console.log('goodbye'); } } //test var a = new Action(); a.leave();
別忘了還可以在類別的內部加入屬性
Function.prototype.sayHello = function(){ console.log('hello'); } Function.prototype.leave = function(){ console.log('leave'); } //test var f = function(){}; f.sayHello();
基類原型拓展,新的一片空間
Function.prototype.addMethod = function(name, fn){ this[name] = fn; } var methods = 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; } var Methods = function(){}; Methods.addMethod('sayHello', function(){ console.log('hello'); }); Methods.addMethod('leave', function(){ console.log('leave'); }); //test var a = new Methods(); a.leave();
原形賦值我們還可以用類別操作
Function.prototype.addMethod = function(name, fn){ this[name] = fn; return this; } var methods = 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; return this; } var Methods = function(){}; Methods.addMethod('sayHello', function(){ console.log('hello'); }).addMethod('leave', function(){ console.log('leave'); }); //test var a = new Methods(); a.leave();
原型 鍊式=更進一步
Function.prototype.addMethod = function(obj){ for(var key in obj){ this[key] = obj[key]; } } var methods = function(){}; methods.addMethod({ sayHello : function(){ console.log('hello'); }, leave : function(){ console.log('goodbye'); } }); //test methods.leave();
添加物件一次做得更多
Function.prototype.addMethod = function(obj){ for(var key in obj){ this.prototype[key] = obj[key]; } } var Methods = function(){}; Methods.addMethod({ sayHello : function(){ console.log('hello'); }, leave : function(){ console.log('goodbye'); } }); //test var a = new Methods(); a.leave();
原型有什麼不可以
Function.prototype.addMethod = function(obj){ for(var key in obj){ this[key] = obj[key]; } return this; } var methods = function(){}; methods.addMethod({ sayHello : function(){ console.log('hello'); } }).addMethod({ leave : function(){ console.log('goodbye'); } }); //test methods.leave();
函數式新增物件也可以鍊式操作
Function.prototype.addMethod = function(obj){ for(var key in obj){ this.prototype[key] = obj[key]; } return this; } var Methods = function(){}; Methods.addMethod({ sayHello : function(){ console.log('hello'); } }).addMethod({ leave : function(){ console.log('goodbye'); } }); //test var a = new Methods(); a.leave();
類別的鍊式操作也可以做得更多
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[key] = arguments[0][key]; } }else if(typeof arguments[0] === "string" && tostring.call(arguments[1]) === '[object Function]'){ this[arguments[0]] = arguments[1]; } return this; }
函數加入封裝一下
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();
追求個人化,這麼做不必說為什麼
以上所述就是本文的全部內容了,希望大家能夠喜歡。