javascript建立函數的20種方式匯總,javascript20種

來源:互聯網
上載者:User

javascript建立函數的20種方式匯總,javascript20種

工作中常常會建立一個函數來解決一些需求問題,以下是個人在工作中總結出來的建立函數20種方式,你知道多少?

function sayHello(){    console.log('hello');}function leave(){    console.log('goodbye');}//testsayHello();

為完成需求,趕緊聲明一個函數吧

 var sayHello = function(){    console.log('hello');}var leave = function(){    console.log('goodbye');}//testleave();

有求必應,函數表達數來解決

 var Action = {    sayHello : function(){        console.log('hello');    },    leave : function(){        console.log('goodbye');    }}//testAction.sayHello();

建立一個方法對象類看起來更整潔

 var Action = function(){};Action.sayHello = function(){    console.log('hello');}Action.leave = function(){    console.log('goodbye');}//testAction.sayHello();

為單體添加屬性方法,淨化命名空間

 var Action = function(){    return {        sayHello : function(){            console.log('hello');        },        leave : function(){            console.log('goodbye');        }    }}// //testvar a = Action();a.leave();

返回新對象我們還有更多的事情可以做

 var Action = function(){};Action.prototype.sayHello = function(){    console.log('hello');}Action.prototype.leave = function(){    console.log('goodbye');}//testvar a = new Action();a.sayHello();

原型鏈指向防止建立多次

 var Action = function(){};Action.prototype = {    sayHello : function(){        console.log('hello');    },    leave : function(){        console.log('goodbye');    }}//testvar a = new Action();a.leave();

對象賦給原型看上去更整潔

 var Action = function(){    this.sayHello = function(){        console.log('hello');    }    this.leave = function(){        console.log('goodbye');    }}//testvar a = new Action();a.leave();

別忘了還可以在類的內部添加屬性

 Function.prototype.sayHello = function(){    console.log('hello');}Function.prototype.leave = function(){    console.log('leave');}//testvar 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');});//testmethods.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');});//testvar 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');});//testmethods.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');});//testvar 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');    }});//testmethods.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');    }});//testvar 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');    }});//testmethods.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');    }});//testvar 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;}//textvar 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();

追求個人化,這麼做不必說為什麼

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.