[英]How to store a JS function reference with an argument included
This Stack post shows how to pass an argument to a JavaScript function reference as part of the .on()
binding process. 这篇Stack文章展示了如何将参数作为
.on()
绑定过程的一部分传递给JavaScript函数引用。 I would like to store the argument with the reference , inside an array, inside an object. 我想将参数与数组内的引用存储在一个对象中。
I have some functions defined at the top of a file: 我在文件顶部定义了一些函数:
function func1() {
$('.result p').text('func1 success');
}
function func2() {
$('.result p').text('func2 success');
}
function func3(someNum) {
$('.result p').text('func3 success, someNum is ' + someNum);
}
I have an object later in that same file: 我后来在同一个文件中有一个对象:
objects[0] = {
name : "Object 1",
funcs : [
func1,
func2,
func3// This is where I want to pass an arg, someNum
]
};
When a certain event happens, I dynamically create 3 buttons, each bound to one of these 3 functions from the object's array: 当某个事件发生时,我动态创建3个按钮,每个按钮绑定到对象数组中的这3个函数之一:
// append a button for each func
funcs.forEach(function(thisFunc) {
$('.buttons').append('<button id="' +
thisFunc.name + '">Run ' +
thisFunc.name + '</button>');
// bind that button to that func
$('#' + thisFunc.name).click(thisFunc);
});
This all works fine with func1
and func2
but I cannot find any documentation on how to pass an argument in this scenario to a function like func3
. 这一切都适用于
func1
和func2
但我找不到任何关于如何将此场景中的参数传递给func3
类的函数的文档。 Ideally, I'd like to store it in the array like so: 理想情况下,我想将它存储在数组中,如下所示:
funcs : [
func1,
func2,
func3(5)
]
But that calls the function (you can add the (5)
into the Fiddle pasted below to see that in action). 但是这会调用函数(你可以将
(5)
添加到下面粘贴的Fiddle中以查看其中的操作)。 I want to bind to the func3
function, with the argument someNum = 5
passed, but only call the function onclick
. 我想绑定到
func3
函数,传递参数someNum = 5
,但只调用函数onclick
。
Demo: 演示:
This Fiddle is a working demo, without the argument being passed to func3
, and that's the part I don't know how to do. 这个小提琴是一个工作演示,没有将参数传递给
func3
,这是我不知道该怎么做的部分。
Also, if my problem lies in the way I am binding, dynamically creating the buttons, or storing the references to the functions, please let me know. 另外,如果我的问题在于我的绑定方式,动态创建按钮或存储对函数的引用,请告诉我。 I am not married to this code structure, this is just the way it seemed proper to build it.
我没有和这个代码结构结合,这只是构建它的方式。
Create a new function using Function.prototype.bind
method: 使用
Function.prototype.bind
方法创建一个新函数:
funcs : [
func1,
func2,
func3.bind(null, 5)
]
bind
will create a new function with the parameter that will be passes in it. bind
将使用将在其中传递的参数创建一个新函数。
Another way is to create an anonymous function, check John Bledsoe's answer. 另一种方法是创建一个匿名函数,检查John Bledsoe的答案。
This should work I believe: 这应该工作我相信:
objects[0] = {
name : "Object 1",
funcs : [
func1,
func2,
function() { return func3(5); }
]
};
You can use also both apply and call when binding to the button 绑定到按钮时,您也可以同时使用apply和call
func.call(null, 5); '5 is just an example, give whatever param you want
or 要么
func.apply(null, [5]);
By 'binding' i mean onclick="func[3].call(...)" in html or $(someobject).click(function(){ func[3].call(...) }). 通过'绑定',我的意思是onclick =“func [3] .call(...)”in html或$(someobject).click(function(){func [3] .call(...)})。
A fiddle especially for my good buddy Lye Fish http://jsfiddle.net/d96ev3c8/8/ who's going to argue some more. 特别是对于我的好朋友Lye Fish http://jsfiddle.net/d96ev3c8/8/ ,这将是一个小提琴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.