imp*_*335 624 javascript parameters function
如何在没有"父"功能中执行的功能或使用eval()?的情况下将函数作为参数传递?(因为我读过它是不安全的.)
我有这个:
addContact(entityId, refreshContactList());
Run Code Online (Sandbox Code Playgroud)
它可以工作,但问题是refreshContactList在调用函数时触发,而不是在函数中使用它时触发.
eval()根据我所读到的内容,我可以使用它,但这不是最好的做法.如何在JavaScript中将函数作为参数传递?
Fen*_*ton 873
您只需要删除括号:
addContact(entityId, refreshContactList);
Run Code Online (Sandbox Code Playgroud)
然后,它会先传递函数而不先执行它.
这是一个例子:
function addContact(id, refreshCallback) {
refreshCallback();
// You can also pass arguments if you need to
// refreshCallback(id);
}
function refreshContactList() {
alert('Hello World');
}
addContact(1, refreshContactList);
Run Code Online (Sandbox Code Playgroud)
dal*_*lin 302
如果要传递函数,只需按名称引用它而不使用括号:
function foo(x) {
alert(x);
}
function bar(func) {
func("Hello World!");
}
//alerts "Hello World!"
bar(foo);
Run Code Online (Sandbox Code Playgroud)
但有时您可能希望传递包含参数的函数,但在调用回调之前不要调用它.要做到这一点,在调用它时,只需将其包装在匿名函数中,如下所示:
function foo(x) {
alert(x);
}
function bar(func) {
func();
}
//alerts "Hello World!" (from within bar AFTER being passed)
bar(function(){ foo("Hello World!") });
Run Code Online (Sandbox Code Playgroud)
如果您愿意,还可以使用apply函数并使用第三个参数作为参数数组,如下所示:
function eat(food1, food2)
{
alert("I like to eat " + food1 + " and " + food2 );
}
function myFunc(callback, args)
{
//do stuff
//...
//execute callback when finished
callback.apply(this, args);
}
//alerts "I like to eat pickles and peanut butter"
myFunc(eat, ["pickles", "peanut butter"]);
Run Code Online (Sandbox Code Playgroud)
小智 51
例1:
funct("z", function (x) { return x; });
function funct(a, foo){
foo(a) // this will return a
}
Run Code Online (Sandbox Code Playgroud)
例2:
function foodemo(value){
return 'hello '+value;
}
function funct(a, foo){
alert(foo(a));
}
//call funct
funct('world!',foodemo); //=> 'hello world!'
Run Code Online (Sandbox Code Playgroud)
nai*_*sts 34
要将函数作为参数传递,只需删除括号!
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 function
if (paramFunc && (typeof paramFunc == "function")) {
paramFunc();
}
}
//this calls iNeedParameter and sends the other function to it
iNeedParameter(ToBeCalled);
Run Code Online (Sandbox Code Playgroud)
这背后的想法是函数与变量非常相似.而不是写作
function ToBeCalled() { /* something */ }
Run Code Online (Sandbox Code Playgroud)
你不妨写一下
var ToBeCalledVariable = function () { /* something */ }
Run Code Online (Sandbox Code Playgroud)
两者之间存在细微差别,但无论如何 - 它们都是定义函数的有效方法.现在,如果您定义一个函数并将其显式分配给变量,那么您可以将其作为参数传递给另一个函数,并且您不需要括号:
anotherFunction(ToBeCalledVariable);
Run Code Online (Sandbox Code Playgroud)
ser*_*0ne 15
JavaScript程序员中有一句话:"Eval is Evil",所以尽量避免它!
除了Steve Fenton的回答,您还可以直接传递函数.
function addContact(entity, refreshFn) {
refreshFn();
}
function callAddContact() {
addContact("entity", function() { DoThis(); });
}
Run Code Online (Sandbox Code Playgroud)
我把那个问题剪掉了。我无法使以上示例正常工作,所以我结束了:
function foo(blabla){
var func = new Function(blabla);
func();
}
// to call it, I just pass the js function I wanted as a string in the new one...
foo("alert('test')");
Run Code Online (Sandbox Code Playgroud)
这就像一种魅力……至少对于我需要的东西。希望对您有所帮助。
我建议将参数放在一个数组中,然后使用该.apply()函数将它们拆分.所以现在我们可以轻松地传递一个包含大量参数的函数,并以一种简单的方式执行它.
function addContact(parameters, refreshCallback) {
refreshCallback.apply(this, parameters);
}
function refreshContactList(int, int, string) {
alert(int + int);
console.log(string);
}
addContact([1,2,"str"], refreshContactList); //parameters should be putted in an array
Run Code Online (Sandbox Code Playgroud)
你也可以eval()用来做同样的事情.
//A function to call
function needToBeCalled(p1, p2)
{
alert(p1+"="+p2);
}
//A function where needToBeCalled passed as an argument with necessary params
//Here params is comma separated string
function callAnotherFunction(aFunction, params)
{
eval(aFunction + "("+params+")");
}
//A function Call
callAnotherFunction("needToBeCalled", "10,20");
Run Code Online (Sandbox Code Playgroud)
而已.我也在寻找这个解决方案并尝试在其他答案中提供的解决方案,但最终从上面的例子中得到了它.
| 归档时间: |
|
| 查看次数: |
597537 次 |
| 最近记录: |