使用javascript.bind()方法跳过参数
在JavaScript中,我可以将参数绑定到这样的函数使用javascript.bind()方法跳过参数,javascript,Javascript,在JavaScript中,我可以将参数绑定到这样的函数 function foo(arg1, arg2) { console.log(arguments); } foo = foo.bind(this, 'bar'); 调用时,我们将获得此输出 foo('baz'); > { '0': 'bar', '1': 'baz' } 是否可以跳过.bind()函数中的参数以实现以下输出 function foo(arg1, arg2, arg3, arg4) { console.log(arg
function foo(arg1, arg2) { console.log(arguments); }
foo = foo.bind(this, 'bar');
调用时,我们将获得此输出
foo('baz');
> { '0': 'bar', '1': 'baz' }
是否可以跳过.bind()函数中的参数以实现以下输出
function foo(arg1, arg2, arg3, arg4) { console.log(arguments); }
foo = foo.bind(this, 'bar', null, 'qux');
foo('baz', 'lel');
> { '0': 'bar', '1': 'baz', '2': 'lel', '3': 'qux' }
不,您只需要绑定
null
函数的参数是函数的参数;绑定一个
null
就是这样做的,它不会重新定义函数,而是返回一个具有相同签名的新函数。不,您只需要绑定null
函数的参数是函数的参数;绑定一个
null
正好做到了这一点,它不会重新定义函数,而是返回一个具有相同签名的新函数。看看下划线的\uu部分
:
通过填充任意数量的参数部分应用函数,而不更改其动态值您可以在参数列表中传递uu,以指定一个不应预先填充的参数,但在调用时保持打开状态以供提供。
就你而言:
function foo(arg1, arg2, arg3, arg4) { console.log(arguments); }
foo = _.partial(foo, 'bar', _, 'qux');
foo('baz', 'lel');
> { '0': 'bar', '1': 'baz', '3': 'qux', '4': 'lel'}
是的,我知道这不是你说的你想要的。您似乎希望两个参数都压缩在所在的位置。除了显式指定调用时将填写两个参数外,没有其他好办法:
foo = _.partial(foo, 'bar', _, _, 'qux');
我不一定建议您使用这个,但至少您可以看看他们的代码并得到一些提示
如果你想让预先确定的参数总是在最后出现,比如'qux'
,你需要一些额外的机制。例如,这里有一个小例程(使用ES6,但您可以进行调整),它将函数转换为一个指定参数位于参数列表末尾的函数:
function partialEnd(fn, ...predefinedArgs) {
return function() {
return fn.apply(this, [...arguments, ...predefinedArgs]);
};
}
您可以这样使用:
function foo(a, b, c) { console.log(a + b + c); }
foo = partialEnd(foo, 3);
foo(1, 2) // outputs 6
您可以将其与使用占位符的\uu.partial
相结合,以获得一种效果,其中一些参数插入参数列表,而另一些参数始终放在末尾:
function foo(arg1, arg2, arg3, arg4) { console.log(arguments); }
foo = _.partial(foo, 'bar', _, _);
foo = partialEnd(foo, 'lel');
foo('baz', 'lel');
> { '0': 'bar', '1': 'baz', '3': 'lel', '4': 'qux'}
看一看下划线的。.partial
:
通过填充任意数量的参数部分应用函数,而不更改其动态值您可以在参数列表中传递uu,以指定一个不应预先填充的参数,但在调用时保持打开状态以供提供。
就你而言:
function foo(arg1, arg2, arg3, arg4) { console.log(arguments); }
foo = _.partial(foo, 'bar', _, 'qux');
foo('baz', 'lel');
> { '0': 'bar', '1': 'baz', '3': 'qux', '4': 'lel'}
是的,我知道这不是你说的你想要的。您似乎希望两个参数都压缩在所在的位置。除了显式指定调用时将填写两个参数外,没有其他好办法:
foo = _.partial(foo, 'bar', _, _, 'qux');
我不一定建议您使用这个,但至少您可以看看他们的代码并得到一些提示
如果你想让预先确定的参数总是在最后出现,比如'qux'
,你需要一些额外的机制。例如,这里有一个小例程(使用ES6,但您可以进行调整),它将函数转换为一个指定参数位于参数列表末尾的函数:
function partialEnd(fn, ...predefinedArgs) {
return function() {
return fn.apply(this, [...arguments, ...predefinedArgs]);
};
}
您可以这样使用:
function foo(a, b, c) { console.log(a + b + c); }
foo = partialEnd(foo, 3);
foo(1, 2) // outputs 6
您可以将其与使用占位符的\uu.partial
相结合,以获得一种效果,其中一些参数插入参数列表,而另一些参数始终放在末尾:
function foo(arg1, arg2, arg3, arg4) { console.log(arguments); }
foo = _.partial(foo, 'bar', _, _);
foo = partialEnd(foo, 'lel');
foo('baz', 'lel');
> { '0': 'bar', '1': 'baz', '3': 'lel', '4': 'qux'}
JavaScript中不存在这样的功能,但是实现一个做同样事情的函数是相当简单的:
function curryWithNulls(fn, thisObj) {
var curriedArgs = Array.prototype.slice.call(arguments, 2);
var length = curriedArgs.length;
return function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < length || args.length; i++) {
if (i >= length || curriedArgs[i] === null) {
curriedArgs[i] = args.shift();
}
}
return fn.apply(thisObj, curriedArgs);
}
}
function foo(arg1, arg2, arg3, arg4) {
console.log(arguments);
}
var curriedFoo = curryWithNulls(foo, this, 'bar', null, null, 'qux');
curriedFoo('baz', 'lel');
// => { '0': 'bar', '1': 'baz', '2': 'lel', '3': 'qux' }
JavaScript中不存在这样的功能,但是实现一个做同样事情的函数是相当简单的:
function curryWithNulls(fn, thisObj) {
var curriedArgs = Array.prototype.slice.call(arguments, 2);
var length = curriedArgs.length;
return function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < length || args.length; i++) {
if (i >= length || curriedArgs[i] === null) {
curriedArgs[i] = args.shift();
}
}
return fn.apply(thisObj, curriedArgs);
}
}
function foo(arg1, arg2, arg3, arg4) {
console.log(arguments);
}
var curriedFoo = curryWithNulls(foo, this, 'bar', null, null, 'qux');
curriedFoo('baz', 'lel');
// => { '0': 'bar', '1': 'baz', '2': 'lel', '3': 'qux' }
可以使用功能性成分:
var foo = function(arg1, arg2, arg3, arg4) { console.log(arguments); }
var bar = function(arg2, arg4) {
return foo.bind(this, 'bar', arg2, 'qux')();
}
bar('baz', 'lel');
或者,使用现代语法:
const foo = (arg1, arg2, arg3, arg4) => console.log(arguments);
const bar = (arg2, arg4) => foo.bind(this, 'bar', arg2, 'qux')();
bar('baz', 'lel');
或者完全没有约束力:
const foo = (arg1, arg2, arg3, arg4) => console.log(arguments);
const bar = (arg2, arg4) => foo(this, 'bar', arg2, 'qux', arg4);
bar('baz', 'lel');
另外,别忘了调用你的bind函数(注意bind之后的这些() 功能性成分可以:
var foo = function(arg1, arg2, arg3, arg4) { console.log(arguments); }
var bar = function(arg2, arg4) {
return foo.bind(this, 'bar', arg2, 'qux')();
}
bar('baz', 'lel');
或者,使用现代语法:
const foo = (arg1, arg2, arg3, arg4) => console.log(arguments);
const bar = (arg2, arg4) => foo.bind(this, 'bar', arg2, 'qux')();
bar('baz', 'lel');
或者完全没有约束力:
const foo = (arg1, arg2, arg3, arg4) => console.log(arguments);
const bar = (arg2, arg4) => foo(this, 'bar', arg2, 'qux', arg4);
bar('baz', 'lel');
另外,别忘了调用你的bind函数(注意bind之后的这些() 我不这么认为。。如果你不需要那个论点,为什么要通过呢?我不这么认为。。如果你不需要这个参数,为什么要传递它“返回一个具有相同签名的新参数”——它实际上是一样的吗?如果绑定参数,新函数的“arity”就会减少。@zerkms是的,我不知道如何使用它。我的意思是传入null不会改变原始函数的arity。“返回一个具有相同签名的新函数”——实际上是相同的吗?如果绑定参数,新函数的“arity”就会减少。@zerkms是的,我不知道如何使用它。我的意思是传入null不会改变原始函数的arity。