javascript创建函数的20种方式汇总
作者:hebedich 发布时间:2024-02-26 21:25:06
标签:javascript,创建函数
工作中常常会创建一个函数来解决一些需求问题,以下是个人在工作中总结出来的创建函数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();
追求个性化,这么做不必说为什么
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- 本文为大家分享了oracle11g安装图文教程,供大家参考,具体内容如下0、安装环境1、安装包下载1)http://www.oracle.c
- public partial class CMS_DBDataContext { partial void OnCreated() { //
- 本文为大家分享了MySQL 5.7版本的安装使用详细教程,更改数据库data的存储路径,供大家参考,具体内容如下因为看到mysql5.7加入
- 由于gitlab的免费私有仓库的优势,所以在公司使用gitlab会多一些,对于gitlab来说,注册需要翻墙,而登录不需要。关于git是做什
- 智能合约1. 是什么智能合约是一种由计算机程序编写的自动化合约,它可以在没有第三方干预的情况下执行交易和契约条款。智能合约使用区块链技术实现
- 对于大前端来说,JS可谓是我们的神器,从页面的效果到数据的传递,再到后台的业务,无处不充斥着JS的身影,但是万能的JS也有贪玩的时候,某一时
- 安装@vitejs/plugin-vue-jsxyarn add -D @vitejs/plugin-vue-jsxnpm i -D @vi
- 今天周五,很闲,坐在电脑前没什么事可做,产品线的人也没提什么新的需求,可能下周会有新的需求和工作安排,但那是下周的事了。今天就想写点技术的东
- 创建变长数组类型CREATE TYPE varray_type AS VARRAY(2) OF VARCHAR2(50);这个变长数组最多可
- 终于下决心把python从2.7升到了3.7。懒人安装当然使用Anaconda。安装成功,编译成功。但是用pip 安装包的时候提示:pip
- CSS制作滑动折叠的文字效果,可以用于二级导航菜单的制作,不错的下拉菜单。<!DOCTYPE html PUBLIC "-/
- 本文实例讲述了Python实现求数列和的方法。分享给大家供大家参考,具体如下:问题:输入输入数据有多组,每组占一行,由两个整数n(n<
- Python编程时,经常需要跳过第一行读取文件内容。简单的做法是为每行设置一个line_num,然后判断line_num是否为1,如果不等于
- 不知不觉的玩了两年多的MySQL,发现很多人都说MySQL对比Oracle来说,优化器做的比较差,其实某种程度上来说确实是这样,
- 本文实例讲述了Python设计模式之工厂方法模式。分享给大家供大家参考,具体如下:工厂方法模式(Factory Method Pattern
- RPC(Remote Procedure Call Protocol)远程过程调用协议。 一个通俗的描述是:客户端在不知道调用细节的情况下,
- Python实现按某一列关键字分组,并计算各列的平均值,并用该值填充该分类该列的nan值。DataFrame数据格式fillna方式实现gr
- 1、异常简介从软件方面来说,错误是语法或是逻辑上的,当python检测到一个错误时,解释器就会指出当前流已经无法继续执行下去,这时候就出现了
- 我们通常会通过单击按钮的操作,将定义好的内容直接复制到剪贴板对于用户来说点了按钮直接【Ctrl】+【V】就可以了。其实该功能的核心原理就是用
- python中有两种方法可以调用父类的方法:super(Child, self).method(args) Parent.meth