javascript Javascript函数指针,参数作为函数中的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13638635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript function pointer with argument as parameter in a function
提问by Jordan Johns
Not sure if the title is worded correctly, or if there is a better way of saying it, but I think its okay.
不确定标题的措辞是否正确,或者是否有更好的表达方式,但我认为还可以。
At any rate, I understand the following thus far:
无论如何,到目前为止,我了解以下内容:
a.b("a", "b", "c", foo);
Where "foo" is a function defined elsewhere that takes in no arguments would simply result in the function a.b() running, with the above parameters. The parameter "foo" can then be called inside function a.b() as simply "foo()". In other words, I understand the above call to be using a function pointer as an argument in the function a.b as a parameter.
其中“foo”是一个在别处定义的函数,它不接受任何参数,只会导致函数 ab() 运行,并带有上述参数。然后可以在函数 ab() 中将参数“foo”简单地称为“foo()”。换句话说,我理解上面的调用是在函数 ab 中使用函数指针作为参数作为参数。
Okay, now here is what I'm trying to do...
好的,现在这就是我想要做的......
I want to be able to do something similar to the above, except this time I want foo to have a paramater passed in that argument as follows:
我希望能够执行与上述类似的操作,但这次我希望 foo 在该参数中传递一个参数,如下所示:
a.b("a", "b", "c", foo("bar"));
Now here is the problem. This will literally result in the paramaters "a", "b", "c" and the result of foo("bar")being used. I don't want this. I want foo("bar") to literally be passed in so that in the function a.b which would look like this (as the header):
现在问题来了。这将逐字地导致参数“a”、“b”、“c”和foo("bar") 的结果被使用。我不要这个。我希望 foo("bar") 从字面上被传入,以便在函数 ab 中看起来像这样(作为标题):
a.b(first, second, third, fourth);
Could reference and call the fourth parameter as:
可以引用并调用第四个参数:
fourth();
Even if "fourth" has an argument in it. I can't seem to find a way around this problem, any advice?
即使“第四”中有一个论点。我似乎找不到解决这个问题的方法,有什么建议吗?
Thanks!
谢谢!
回答by I Hate Lazy
Use an anonymous function to wrap your foo
call.
使用匿名函数来封装您的foo
调用。
a.b("a", "b", "c", function() {return foo("bar");});
If you need to retain the this
value that would be given, you can invoke it using .call
. You can also pass along any arguments given.
如果您需要保留this
将给定的值,您可以使用.call
. 您还可以传递给定的任何参数。
a.b("a", "b", "c", function(arg1, arg2) {return foo.call(this, "bar", arg1, arg2);});
And of course the function doesn't necessarily need to be anonymous. You can use a named function just as well.
当然,该函数不一定需要匿名。您也可以使用命名函数。
function bar(arg1, arg2) {
return foo.call(this, "bar", arg1, arg2);
}
a.b("a", "b", "c", bar);
回答by Roman
It's really easy to use BIND with function
使用 BIND with function 真的很容易
a.b("a", "b", "c", foo.bind(undefined, "bar"));
So when you call foo()
inside your function it already has the first binded argument. You can apply as more argumenst as you wish using bind
.
因此,当您foo()
在函数内部调用时,它已经具有第一个绑定参数。您可以使用bind
.
Note that bind
allways apply arguments to function and place them first. If you want to apply to the and use this:
请注意,bind
始终将参数应用于函数并将它们放在首位。如果你想申请并使用这个:
if (!Function.prototype.bindBack) {
Function.prototype.bindBack = function (_super) {
if (typeof this !== "function")
throw new TypeError("Function.prototype.bindBack - can not by prototyped");
var additionalArgs = Array.prototype.slice.call(arguments, 1),
_this_super = this,
_notPrototyped = function () {},
_ref = function () {
return _this_super.apply((this instanceof _notPrototyped && _super) ? this : _super, (Array.prototype.slice.call(arguments)).concat(additionalArgs));
};
_notPrototyped.prototype = this.prototype;
_ref.prototype = new _notPrototyped();
return _ref;
}
}
function tracer(param1, param2, param3) {
console.log(arguments)
}
function starter(callback) {
callback('starter 01', 'starter 02')
}
// See here!!!
// function starter call 'calback' with just 2 params, to add 3+ params use function.bindBack(undefined, param, param, ...)
starter(tracer.bindBack(undefined, 'init value'));
See example http://jsfiddle.net/zafod/YxBf9/2/