微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript创建函数的20种方式汇总

工作中常常会创建一个函数解决一些需求问题,以下是个人在工作中总结出来的创建函数20种方式,你知道多少?

rush:js;"> function sayHello(){ console.log('hello'); } function leave(){ console.log('goodbye'); } //test sayHello();

为完成需求,赶紧声明一个函数

rush:js;">

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

有求必应,函数表达数来解决

rush:js;">

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

创建一个方法对象类看起来更整洁

rush:js;">

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

为单体添加属性方法,净化命名空间

rush:js;">

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

返回新对象我们还有更多的事情可以做

rush:js;">

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

原型链指向防止创建多次

rush:js;">

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

对象赋给原型看上去更整洁

rush:js;">

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

别忘了还可以在类的内部添加属性

rush:js;">

Function.prototype.sayHello = function(){
console.log('hello');
}
Function.prototype.leave = function(){
console.log('leave');
}
//test
var f = function(){};
f.sayHello();

基类原型拓展,新的一片空间

rush:js;">

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

通用定义方法函数使用更方便

rush:js;">

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

原形赋值我们还可以用类操作

rush:js;"> 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();

链式操作有何不可

rush:js;">

Function.prototype.addMethod = function(name,fn){
this.prototype[name] = fn;
return this;
}
var Methods = function(){};
Methods.addMethod('sayHello',function(){
console.log('leave');
});
//test
var a = new Methods();
a.leave();

原型+链式=更进一步

rush:js;">

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

添加对象一次做得更多

rush:js;">

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

原型有什么不可以

rush:js;">

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

函数添加对象也可以链式操作

rush:js;">

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

类的链式操作也可以做得更多

rush:js;">

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

函数添加封装一下

rush:js;">

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

类式添加追求的就是个性化

rush:js;">

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

追求个性化,这么做不必说为什么

以上所述就是本文的全部内容了,希望大家能够喜欢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据复制操作的两种方式。‌在聊深浅拷贝之前咱得了解一下js中的两种数据类型:
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:重建二叉树、反向输出链表每个节点 题目 重建二叉树: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列 {1,2,4,7,3,5,6,8} 和中序遍历序列 {
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须了解的知识点,现在迎来了ES6+的时代,因为箭头函数的出现,所以感觉有必要对 this 问题梳理一下,所以刚好总结一下JavaScript中this指向的问题。
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高