更好地理解 javascript 中的回调函数
我正在书上读这个函数,但我不明白匿名函数是如何实现的 是通过了口渴三论吗?
这是我的理解
function multiplyByTwo(a, b, c, callback) {
var i, ar = [];
for(i = 0; i < 3; i++) {
ar[i] = callback(arguments[i] * 2);
}
return ar;
}
,但不是这个
myarr = multiplyByTwo(1, 2, 3, function(a){return a + 1});
,谢谢,理查德
I am reading this function in a book, and I don't understand how the anonymous function
is passed the thirst three arguments?
This is what I understand
function multiplyByTwo(a, b, c, callback) {
var i, ar = [];
for(i = 0; i < 3; i++) {
ar[i] = callback(arguments[i] * 2);
}
return ar;
}
but not this
myarr = multiplyByTwo(1, 2, 3, function(a){return a + 1});
thanks, Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些详细说明
multiplyByTwo
函数约定的注释在这里会很有帮助:这意味着该函数接受四个参数,并返回一个
x
数组。x
可以是任何类型。前三个参数是数字。函数的第四个参数实际上是函数本身。该函数接受一个数字,并返回一个x
。同样,x
可以是任何类型,但它在整个合约中都是相同的类型。在
multiplyByTwo
函数内,为每个其他参数调用一次回调函数。每个调用的结果都会添加到一个数组中,即multiplyByTwo
的返回值。arguments
变量是 javascript 中的一个特殊变量,它在函数作用域内以数组形式提供对该函数的所有参数的访问。回调函数绑定到
multiplyByTwo
函数范围内的名称callback
。然后可以像任何其他函数一样调用它,例如通过附加括号和参数。然后可以通过该名称引用您在调用multiplyByTwo
时提供的匿名函数。因此,在给出的示例中,使用四个参数调用
multiplyByTwo
:1
、2
、3
和一个匿名函数。在multiplyByTwo
函数的范围内,这些参数绑定到变量a
、b
、c
和分别是回调
。这些参数也可以通过特殊的arguments
数组引用。在示例中,这两种方法都用于引用参数。前三个参数使用循环内的arguments
数组进行引用,第四个参数使用其名称进行引用,如callback(...)
中所示。Some comments detailing the contract of the
multiplyByTwo
function would be helpful here:This means that the function takes four arguments, and returns an array of
x
.x
can be any type. The first three arguments are numbers. The fourth argument to the function is actually a function itself. This function takes a number, and returns anx
. Again,x
can be any type, but it is the same type throughout the contract.Inside the
multiplyByTwo
function, the callback function is called once for each of the other arguments. The result of each of these calls is added to an array, which is the return value ofmultiplyByTwo
.The
arguments
variable is a special variable in javascript, which, inside the scope of a function, gives access to all of the arguments to that function, as an array.The callback function is bound to the name
callback
in the scope of themultiplyByTwo
function. It can then be called as any other function, such as by appending parentheses and arguments. The anonymous function that you gave when you calledmultiplyByTwo
can then be referenced by that name.So, in the example given,
multiplyByTwo
is called with four arguments:1
,2
,3
, and an anonymous function. Inside the scope of themultiplyByTwo
function, these arguments are bound to the variablesa
,b
,c
, andcallback
, respectively. These arguments can also be referenced through the specialarguments
array. In the example, both methods are used to reference the arguments. The first three are referenced using thearguments
array inside a loop, and the fourth argument is referenced using its name, as incallback(...)
.匿名函数在循环中被调用 3 次,每次传递不同的参数。第一次传入
arguments[0]
,即a
(而arguments[1]
将为b
> 等)。arguments
是当前执行函数的属性,指传递到函数中的参数。在 JavaScript 中,您不必为参数命名,也不必通过名称访问它们,甚至不必传入所有命名参数。The anonymous function is called 3 times in the loop, each time passing in a different argument. The first time it passes in
arguments[0]
, which isa
(andarguments[1]
would beb
, etc.).arguments
is a property of the currently executing function and refers to the parameters passed into the function. In JavaScript, you don't have to name your parameters, or access them by their names, or even pass in all of the named parameters.