javascript创建函数的20种方式汇总
作者:hebedich 时间:2024-02-26 21:25:06
工作中常常会创建一个函数来解决一些需求问题,以下是个人在工作中总结出来的创建函数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();
追求个性化,这么做不必说为什么
以上所述就是本文的全部内容了,希望大家能够喜欢。
标签:javascript,创建函数
0
投稿
猜你喜欢
matlab中二维插值函数interp2的使用详解
2023-08-11 00:28:45
Python list sort方法的具体使用
2023-01-14 22:27:46
ASP中实现分页显示的七种方法
2007-09-20 13:19:00
Python数据分析之NumPy常用函数使用详解
2021-09-29 06:17:33
SQL 统计一个数据库中所有表记录的数量
2012-01-29 18:21:36
SQL Server之JSON 函数详解
2024-01-12 14:10:20
Python文本处理之按行处理大文件的方法
2023-01-24 11:11:34
Python中摘要算法MD5,SHA1简介及应用实例代码
2023-12-12 00:36:41
Python ARP扫描与欺骗实现全程详解
2021-12-16 09:01:14
网页制作了解什么是CSS hack?
2007-10-21 08:52:00
js实现将选中内容分享到新浪或腾讯微博
2023-08-25 07:39:02
5款Python程序员高频使用开发工具推荐
2022-01-25 14:09:16
解决python运行启动报错问题
2022-08-13 06:44:15
Django配置跨域并开发测试接口
2022-03-20 21:29:08
pydantic resolve解决嵌套数据结构生成痛点分析
2022-06-05 02:16:36
深度解析Django REST Framework 批量操作
2021-05-18 03:03:21
详解Python的三种拷贝方式
2023-04-20 19:39:20
Python实现一维插值方法的示例代码
2022-04-14 02:49:10
Python组合数据类型详解
2022-10-03 02:23:47
Python networkx包的实现
2023-07-29 23:58:03