本文翻译自:Pass a JavaScript function as parameter

How do I pass a function as a parameter without the function executing in the "parent" function or using eval() ? 如何在没有“父”函数中执行的函数或使用eval()情况下将函数作为参数传递? (Since I've read that it's insecure.) (因为我读过它是不安全的。)

I have this: 我有这个:

addContact(entityId, refreshContactList());

It works, but the problem is that refreshContactList fires when the function is called, rather than when it's used in the function. 它可以工作,但问题是refreshContactList在调用函数时触发,而不是在函数中使用它时触发。

I could get around it using eval() , but it's not the best practice, according to what I've read. 我可以使用eval()绕过它,但根据我所读到的,这不是最好的做法。 How can I pass a function as a parameter in JavaScript? 如何在JavaScript中将函数作为参数传递?


#1楼

参考:https://stackoom.com/question/tkM5/将JavaScript函数作为参数传递


#2楼

You just need to remove the parenthesis: 您只需要删除括号:

addContact(entityId, refreshContactList);

This then passes the function without executing it first. 然后,它会先传递函数而不先执行它。

Here is an example: 这是一个例子:

function addContact(id, refreshCallback) {refreshCallback();// You can also pass arguments if you need to// refreshCallback(id);
}function refreshContactList() {alert('Hello World');
}addContact(1, refreshContactList);

#3楼

To pass the function as parameter, simply remove the brackets! 要将函数作为参数传递,只需删除括号!

function ToBeCalled(){alert("I was called");
}function iNeedParameter( paramFunc) {//it is a good idea to check if the parameter is actually not null//and that it is a functionif (paramFunc && (typeof paramFunc == "function")) {paramFunc();   }
}//this calls iNeedParameter and sends the other function to it
iNeedParameter(ToBeCalled);

The idea behind this is that a function is quite similar to a variable. 这背后的想法是函数与变量非常相似。 Instead of writing 而不是写作

function ToBeCalled() { /* something */ }

you might as well write 你不妨写一下

var ToBeCalledVariable = function () { /* something */ }

There are minor differences between the two, but anyway - both of them are valid ways to define a function. 两者之间存在细微差别,但无论如何 - 它们都是定义函数的有效方法。 Now, if you define a function and explicitly assign it to a variable, it seems quite logical, that you can pass it as parameter to another function, and you don't need brackets: 现在,如果您定义一个函数并将其显式分配给变量,那么您可以将其作为参数传递给另一个函数,并且您不需要括号:

anotherFunction(ToBeCalledVariable);

#4楼

Example 1: 例1:

funct("z", function (x) { return x; });function funct(a, foo){foo(a) // this will return a
}

Example 2: 例2:

function foodemo(value){return 'hello '+value;
}function funct(a, foo){alert(foo(a));
}//call funct
funct('world!',foodemo); //=> 'hello world!'

look at this 看这个


#5楼

There is a phrase amongst JavaScript programmers: "Eval is Evil" so try to avoid it at all costs! JavaScript程序员中有一句话:“Eval is Evil”,所以尽量避免它!

In addition to Steve Fenton's answer, you can also pass functions directly. 除了Steve Fenton的回答,您还可以直接传递函数。

function addContact(entity, refreshFn) {refreshFn();
}function callAddContact() {addContact("entity", function() { DoThis(); });
}

#6楼

You can use a JSON as well to store and send JS functions. 您也可以使用JSON来存储和发送JS函数。

Check the following: 检查以下内容:

var myJSON =
{"myFunc1" : function (){alert("a");}, "myFunc2" : function (functionParameter){functionParameter();}
}function main(){myJSON.myFunc2(myJSON.myFunc1);
}

This will print 'a'. 这将打印'a'。

The following has the same effect with the above: 以下与上述效果相同:

var myFunc1 = function (){alert('a');
}var myFunc2 = function (functionParameter){functionParameter();
}function main(){myFunc2(myFunc1);
}

Which is also has the same effect with the following: 这也与以下内容具有相同的效果:

function myFunc1(){alert('a');
}function myFunc2 (functionParameter){functionParameter();
}function main(){myFunc2(myFunc1);
}

And a object paradigm using Class as object prototype: 以及使用Class作为对象原型的对象范例:

function Class(){this.myFunc1 =  function(msg){alert(msg);}this.myFunc2 = function(callBackParameter){callBackParameter('message');}
}function main(){    var myClass = new Class();  myClass.myFunc2(myClass.myFunc1);
}

将JavaScript函数作为参数传递相关推荐

  1. JavaScript中函数当作参数传递或当作返回值

    2019独角兽企业重金招聘Python工程师标准>>> 在原生JS中函数既可以当作参数来传递,也可以当作返回值来使用: 下面是我初学js时为了更好理解这个概念写的一个小片段: 备注: ...

  2. 深入理解javascript函数系列第二篇——函数参数

    前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传进来的参数是什么数据类型,甚至可以不传参数.本文是深入理解javascript函数 ...

  3. javaScript系列 [01]-javaScript函数基础

    [01]-javaScript函数基础 1.1 函数的创建和结构 函数的定义:函数是JavaScript的基础模块单元,包含一组语句,用于代码复用.信息隐蔽和组合调用. 函数的创建:在javaScri ...

  4. html表单文本框怎么输出函数值,如何获取用户输入的html文本表单字段传递给javascript函数的值?...

    我想通过生成用户必须输入到文本输入表单字段的随机数创建我自己的反垃圾邮件过滤器,如果它是正确的,他们进入下一页,如果不是,则显示错误数字输入不正确.如何获取用户输入的html文本表单字段传递给java ...

  5. 盘点JavaScript函数的基本知识

    本文旨在提供web开发人员必须了解的所有JavaScript函数的基本知识. 函数于软件开发者而言并不是什么奇幻世界.如果你的日常活动涉及到编码,哪怕是一点点,那么在一天结束的时候,你一定创建/修改了 ...

  6. js进阶ajax函数封装(匿名函数作为参数传递)(封装函数引入文件的方式非常好用)...

    js进阶ajax函数封装(匿名函数作为参数传递)(封装函数引入文件的方式非常好用) 一.总结 2.匿名函数作为参数传递 二.js进阶ajax函数封装 ajax1.js 1 function ajax( ...

  7. 深入理解javascript函数进阶系列第一篇——高阶函数

    前面的话 前面的函数系列中介绍了函数的基础用法.从本文开始,将介绍javascript函数进阶系列,本文将详细介绍高阶函数 定义 高阶函数(higher-order function)指操作函数的函数 ...

  8. JavaScript(三)—— JavaScript 函数/JavaScript 作用域/JavaScript 预解析/JavaScript 对象

    本篇为 JavaScript 系列笔记第三篇,将陆续更新 JavaScript(一)-- 初识JavaScript/注释/输入输出语句/变量/数据类型 JavaScript(二)-- JavaScri ...

  9. 深入探讨JavaScript函数

    目录 介绍 什么是JavaScript函数? 定义函数 函数声明 函数表达式 箭头函数 函数提升 一流函数的属性 函数签名 重载 参数 默认参数 ES6之前的默认参数 默认参数ES6 函数类型 函数属 ...

最新文章

  1. hbase 0.96 java api_HBase(九) HBase JAVA API - 运维API
  2. 深入浅出SharePoint2013——获取Application Pool的id和name对照表
  3. Loudrunner常用函数
  4. pmp知识点详解-项目大牛整理_PMP核心知识点第六章:项目进度管理(3)
  5. oa部署mysql_oa系统部署
  6. 华为NFV实验室一周年:打造开放合作生态链 加速NFV产业化
  7. (译)如何使用cocos2d制作基于tile地图的游戏教程:第一部分
  8. 推文科技:AI解决方案助力内容出海
  9. 【zepto学习笔记02】零碎点
  10. 从头开发一个Flutter插件(一)开发流程
  11. android.util.typedvalue的jar包,android – 了解Typed值类
  12. 一个示例让你明白界面与数据分离
  13. 组态软件android版,昆仑通态组态软件
  14. 现代优化算法:遗传算法
  15. 50个最常用的Unix/Linux命令
  16. html的实习报告,HTML实习报告
  17. Lucas-Kanade 20 Years On 正反向/累加/合成求解算法
  18. 【Pycharm】主题背景颜色更改
  19. linux 显示bin 文件格式,bin文件扩展名,bin文件怎么打开?
  20. 安卓中的对称加密,非对称加密,MD5加密的算法

热门文章

  1. Thymeleaf模板的使用
  2. Android binder学习一:主要概念
  3. 2003系统安全配置
  4. 多线程专题之线程同步(1)
  5. 关于phpexcel读取时间字段的格式不正确
  6. 微信小程序遇到的那些坑
  7. BaseAnimation
  8. freemarker取数
  9. 利用Microsoft.VisualBasic dll来修改系统时间
  10. Ubuntu无法进入操作系统的恢复和备份操作