国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript創(chuàng)建函數(shù)的20種方式匯總

2019-11-20 12:11:39
字體:
供稿:網(wǎng)友

工作中常常會創(chuàng)建一個(gè)函數(shù)來解決一些需求問題,以下是個(gè)人在工作中總結(jié)出來的創(chuàng)建函數(shù)20種方式,你知道多少?

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

為完成需求,趕緊聲明一個(gè)函數(shù)吧

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

有求必應(yīng),函數(shù)表達(dá)數(shù)來解決

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

創(chuàng)建一個(gè)方法對象類看起來更整潔

 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();

原型鏈指向防止創(chuàng)建多次

 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();

別忘了還可以在類的內(nèi)部添加屬性

 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();

通用定義方法函數(shù)使用更方便

 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();

鏈?zhǔn)讲僮饔泻尾豢?br />

 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();

原型+鏈?zhǔn)?更進(jìn)一步

 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();

函數(shù)式添加對象也可以鏈?zhǔn)讲僮?br />

 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();

類的鏈?zhǔn)讲僮饕部梢宰龅酶?br />

 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;}

函數(shù)添加封裝一下

 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;}

類式添加追求的就是個(gè)性化

 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();

追求個(gè)性化,這么做不必說為什么

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 邮箱| 台湾省| 宁乡县| 汾阳市| 渭源县| 永福县| 济宁市| 三门县| 光泽县| 萨迦县| 阿拉善左旗| 昌图县| 通榆县| 贵州省| 徐闻县| 乾安县| 定远县| 兴文县| 武陟县| 广东省| 都安| 萍乡市| 湟中县| 江都市| 滕州市| 湛江市| 绍兴市| 安陆市| 茌平县| 道孚县| 宁武县| 福建省| 巨鹿县| 高州市| 永胜县| 集安市| 泊头市| 孝昌县| 灵丘县| 星子县| 河西区|