如何绑定函数参数而不绑定它? [英] How to bind function arguments without binding this?

查看:113
本文介绍了如何绑定函数参数而不绑定它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,如何在不绑定这个参数的情况下将参数绑定到函数?

In Javascript, how can I bind arguments to a function without binding the this parameter?

例如:

//Example function.
var c = function(a, b, c, callback) {};

//Bind values 1, 2, and 3 to a, b, and c, leave callback unbound.
var b = c.bind(null, 1, 2, 3); //How can I do this without binding scope?

如何避免必须绑定函数范围的副作用(例如设置 = null)?

How can I avoid the side-effect of having to bind the function's scope (e.g. setting this = null) as well?

修改:

很抱歉这个混乱。我想绑定参数,然后能够稍后调用绑定函数并使其行为与我调用原始函数并将其传递给绑定参数完全相同:

Sorry for the confusion. I want to bind arguments, then be able to call the bound function later and have it behave exactly as if I called the original function and passed it the bound arguments:

var x = 'outside object';

var obj = {
  x: 'inside object',
  c: function(a, b, c, callback) {
    console.log(this.x);
  }
};

var b = obj.c.bind(null, 1, 2, 3);

//These should both have exact same output.
obj.c(1, 2, 3, function(){});
b(function(){});

//The following works, but I was hoping there was a better way:
var b = obj.c.bind(obj, 1, 2, 3); //Anyway to make it work without typing obj twice?

我还是新手,很抱歉这个混乱。

I'm still new at this, sorry for the confusion.

谢谢!

推荐答案

您可以这样做,但最好不要将其视为绑定因为那是用于设置this值的术语。也许可以把它看作是将参数包装到函数中?

You can do this, but best to avoid thinking of it as "binding" since that is the term used for setting the "this" value. Perhaps think of it as "wrapping" the arguments into a function?

你要做的是创建一个函数,通过闭包在其中内置所需的参数:

What you do is create a function that has the desired arguments built into it via closures:

var withWrappedArguments = function(arg1, arg2)
    {
        return function() { ... do your stuff with arg1 and arg2 ... };
    }(actualArg1Value, actualArg2Value);

希望我能在那里得到语法。它的作用是创建一个名为withWrappedArguments()的函数(这是一个迂腐的,它是一个分配给变量的匿名函数),你可以在任何地方随时调用它,并且总是使用actualArg1Value和actualArg2Value以及你想放入的任何其他内容。那里。如果需要,您还可以在通话时接受进一步的参数。秘密是最后一个结束后的括号。这些导致使用传递的值立即执行外部函数,并生成可以在以后调用的内部函数。然后在生成函数时冻结传递的值。

Hope I got the syntax right there. What it does is create a function called withWrappedArguments() (to be pedantic it is an anonymous function assigned to the variable) that you can call any time any where and will always act with actualArg1Value and actualArg2Value, and anything else you want to put in there. You can also have it accept further arguments at the time of the call if you want. The secret is the parentheses after the final closing brace. These cause the outer function to be immediately executed, with the passed values, and to generate the inner function that can be called later. The passed values are then frozen at the time the function is generated.

这实际上是绑定的作用,但这样显然包装的参数只是闭包局部变量,没有必要改变它的行为。

This is effectively what bind does, but this way it is explicit that the wrapped arguments are simply closures on local variables, and there is no need to change the behaviour of this.

这篇关于如何绑定函数参数而不绑定它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆