在javascript中将函数绑定到另一个函数

时间:2022-01-23 20:35:30

I have a function in javascript

我在javascript中有一个功能

function foo(callback) {
    console.log("Hello");
    callback();
}

and another function

和另一个功能

function bar() {
  console.log("world");
}

and I want to make a function FooBar

我想制作一个FooBar函数

FooBar = foo.bind(this, bar);

This works fine, however what I'm actually trying to do is create a function queue, and often I will have to bind a none function parameter before binding a callback, as in the following example

这工作正常,但我实际上要做的是创建一个函数队列,并且在绑定回调之前我经常需要绑定一个无函数参数,如下例所示

function foo() {
    console.log(arguments[0]);
    var func = arguments[1];
    func();
}

function bar() {
    console.log("world");
}

foo.bind(this, "hello");
var FooBar = foo.bind(this, bar);

FooBar();

which produces this error

这会产生这个错误

[Function: bar]

TypeError: undefined is not a function

How can a I bind a function to another function once it has been bound to other none function types?

一旦将函数绑定到其他函数类型,我如何将函数绑定到另一个函数?

2 个解决方案

#1


5  

You're binding "Hello" to foo, and then separately binding bar to foo as well - shouldn't you bind bar to the result of the first bind, like this:

你将“Hello”绑定到foo,然后将bar单独绑定到foo - 不应该将bar绑定到第一个绑定的结果,如下所示:

var FooHello = foo.bind(this, "hello");
var FooBar = FooHello.bind(this, bar);

Fiddle here. (which logs "Hello", "world").

在这里小提琴。 (记录“你好”,“世界”)。

#2


4  

The bind method doesn't bind functions to one another, it's purpose is to fix the context of the 'this' keyword and any arguments when the function is called. It then returns a new function, leaving the original unchanged.

bind方法不会将函数绑定到彼此,它的目的是在调用函数时修复'this'关键字的上下文和任何参数。然后它返回一个新函数,保持原始状态不变。

So: foo.bind(this, "hello")actually has no effect. Calling bind the second time creates a new function with a fixed argument of bar. The reason you get an error is because with only one argument passed, arguments[1] is undefined.

所以:foo.bind(这个,“你好”)实际上没有效果。第二次调用bind会创建一个带有固定参数bar的新函数。你得到错误的原因是因为只传递了一个参数,所以参数[1]是未定义的。

You could do as Richie suggests and add an intermediate variable, or just pass both arguments in a single bind:

您可以像Richie建议并添加一个中间变量,或者只在一个绑定中传递两个参数:

var FooBar = foo.bind(this, "hello", bar);

It is probably a good idea to also include run time checking so your function can detect when it is dealing with a function. This will save you having to worry about argument order.

最好还包括运行时检查,以便您的函数可以检测它何时处理函数。这将节省您不必担心参数顺序。

if(typeof func === 'function'){
  func();
}

#1


5  

You're binding "Hello" to foo, and then separately binding bar to foo as well - shouldn't you bind bar to the result of the first bind, like this:

你将“Hello”绑定到foo,然后将bar单独绑定到foo - 不应该将bar绑定到第一个绑定的结果,如下所示:

var FooHello = foo.bind(this, "hello");
var FooBar = FooHello.bind(this, bar);

Fiddle here. (which logs "Hello", "world").

在这里小提琴。 (记录“你好”,“世界”)。

#2


4  

The bind method doesn't bind functions to one another, it's purpose is to fix the context of the 'this' keyword and any arguments when the function is called. It then returns a new function, leaving the original unchanged.

bind方法不会将函数绑定到彼此,它的目的是在调用函数时修复'this'关键字的上下文和任何参数。然后它返回一个新函数,保持原始状态不变。

So: foo.bind(this, "hello")actually has no effect. Calling bind the second time creates a new function with a fixed argument of bar. The reason you get an error is because with only one argument passed, arguments[1] is undefined.

所以:foo.bind(这个,“你好”)实际上没有效果。第二次调用bind会创建一个带有固定参数bar的新函数。你得到错误的原因是因为只传递了一个参数,所以参数[1]是未定义的。

You could do as Richie suggests and add an intermediate variable, or just pass both arguments in a single bind:

您可以像Richie建议并添加一个中间变量,或者只在一个绑定中传递两个参数:

var FooBar = foo.bind(this, "hello", bar);

It is probably a good idea to also include run time checking so your function can detect when it is dealing with a function. This will save you having to worry about argument order.

最好还包括运行时检查,以便您的函数可以检测它何时处理函数。这将节省您不必担心参数顺序。

if(typeof func === 'function'){
  func();
}