函数中参数为参数的Javascript函数指针
我不确定标题的措辞是否正确,或者是否有更好的表达方式,但我认为没关系 无论如何,到目前为止,我了解以下情况:函数中参数为参数的Javascript函数指针,javascript,function,parameter-passing,function-pointers,Javascript,Function,Parameter Passing,Function Pointers,我不确定标题的措辞是否正确,或者是否有更好的表达方式,但我认为没关系 无论如何,到目前为止,我了解以下情况: a.b("a", "b", "c", foo); 其中“foo”是在别处定义的一个函数,它不接受任何参数,只会导致函数a.b()运行,并带有上述参数。参数“foo”可以在函数a.b()中简单地称为“foo()”。换句话说,我理解上面的调用将函数指针用作函数a.b中的参数 好吧,现在我要做的是 我希望能够执行与上述类似的操作,但这次我希望foo在该参数中传递一个参数,如下所示: a.b(
a.b("a", "b", "c", foo);
其中“foo”是在别处定义的一个函数,它不接受任何参数,只会导致函数a.b()运行,并带有上述参数。参数“foo”可以在函数a.b()中简单地称为“foo()”。换句话说,我理解上面的调用将函数指针用作函数a.b中的参数
好吧,现在我要做的是
我希望能够执行与上述类似的操作,但这次我希望foo在该参数中传递一个参数,如下所示:
a.b("a", "b", "c", foo("bar"));
现在问题来了。这将直接导致使用参数“a”、“b”、“c”和foo(“bar”)的结果。我不要这个。我希望传入foo(“bar”),以便在函数a.b中(作为标题)如下所示:
可以引用并调用第四个参数,如下所示:
fourth();
即使“第四个”里面有个论点。我似乎找不到解决这个问题的办法,有什么建议吗
谢谢 使用匿名函数包装您的
foo
调用
a.b("a", "b", "c", function() {return foo("bar");});
如果需要保留将给出的
值,可以使用调用。您还可以传递给定的任何参数
a.b("a", "b", "c", function(arg1, arg2) {return foo.call(this, "bar", arg1, arg2);});
当然,函数不一定是匿名的。您也可以使用命名函数
function bar(arg1, arg2) {
return foo.call(this, "bar", arg1, arg2);
}
a.b("a", "b", "c", bar);
绑定函数非常容易使用
a.b("a", "b", "c", foo.bind(undefined, "bar"));
因此,当您在函数中调用foo()
时,它已经有了第一个绑定参数。您可以使用bind
应用任意多的参数
请注意,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'));
参见示例这非常有帮助,确实解决了问题。非常感谢。(我会尽快接受你的回答,让我…6分钟:)
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'));