JavaScript 函数中的参数处理
Parameters handling in JavaScript functions
目前我正在阅读 JavaScript 本书。其中有一个我无法理解的代码片段。 repeat(3, function(n) {
行发生了什么?为什么我们可以将参数n
传递给函数repeat
的第二个参数,因为在它的声明中没有关于传递参数的内容? repeat
如何理解它应该将参数 n
传递给 unless
函数?
function unless(test, then) {
if (!test) then();
}
function repeat(times, body) {
for (var i = 0; i < times; i++) body(i);
}
repeat(3, function(n) {
unless(n % 2, function() {
console.log(n, "is even");
});
});
// → 0 is even
// → 2 is even
您根本没有传递参数 n
。
实际上,您将整个匿名函数作为参数传递(函数是 JavaScript 中的第一个 class 公民,可以像其他变量一样传递)。
如果您看一下,该函数作为 body
参数传递给方法 repeat
。 repeat
然后调用函数body
,参数为i
...也就是匿名函数中的参数n
。
Why we can pass parameter n to the second argument of the function repeat, because in its declaration there is nothing about passing parameters
你不是将n
作为第二个参数传递给repeat()
,你传递的是一个anonymous function,它接受一个参数并且您选择命名 its 参数 n
(因此传入函数的参数)
JavaScript中的函数,简单来说就是可以执行的对象。这意味着您可以将函数作为参数传递给其他函数,或者像对对象一样向它们添加属性等。
以下是您的示例中发生的情况的说明:
函数 repeat
定义了两个参数:
repeat(times, body)
所以,您所做的只是传递一个function
作为body
参数。
这样写就等价于:
var times = 3;
var body = function(n) {
unless(n % 2, function() {
console.log(n, "is even");
});
};
repeat(time, body);
How does repeat understand that it should pass parameter n to the unless function?
正如您在上面看到的,repeat
没有将任何内容传递给 unless()
。
实际上将 n
传递给 unless
.
的是您的匿名函数(在我上面的示例中存储在 body
中)
如果你只是想写一个函数来重复 x 次,并检查它是否是偶数,你可能想这样做:
function repeat(times) {
for (var i = 0; i < times; i++) {
output.innerHTML+= is_even(i)+"\n";
}
}
function is_even(n) {
if ( n % 2 ) return n+" is even";
return n+" is odd";
}
var output = document.getElementById('output');
repeat(6); // Output variable wasn't passed to this function, we're using it globally
<pre id='output'></pre>
目前我正在阅读 JavaScript 本书。其中有一个我无法理解的代码片段。 repeat(3, function(n) {
行发生了什么?为什么我们可以将参数n
传递给函数repeat
的第二个参数,因为在它的声明中没有关于传递参数的内容? repeat
如何理解它应该将参数 n
传递给 unless
函数?
function unless(test, then) {
if (!test) then();
}
function repeat(times, body) {
for (var i = 0; i < times; i++) body(i);
}
repeat(3, function(n) {
unless(n % 2, function() {
console.log(n, "is even");
});
});
// → 0 is even
// → 2 is even
您根本没有传递参数 n
。
实际上,您将整个匿名函数作为参数传递(函数是 JavaScript 中的第一个 class 公民,可以像其他变量一样传递)。
如果您看一下,该函数作为 body
参数传递给方法 repeat
。 repeat
然后调用函数body
,参数为i
...也就是匿名函数中的参数n
。
Why we can pass parameter n to the second argument of the function repeat, because in its declaration there is nothing about passing parameters
你不是将n
作为第二个参数传递给repeat()
,你传递的是一个anonymous function,它接受一个参数并且您选择命名 its 参数 n
(因此传入函数的参数)
JavaScript中的函数,简单来说就是可以执行的对象。这意味着您可以将函数作为参数传递给其他函数,或者像对对象一样向它们添加属性等。
以下是您的示例中发生的情况的说明:
函数 repeat
定义了两个参数:
repeat(times, body)
所以,您所做的只是传递一个function
作为body
参数。
这样写就等价于:
var times = 3;
var body = function(n) {
unless(n % 2, function() {
console.log(n, "is even");
});
};
repeat(time, body);
How does repeat understand that it should pass parameter n to the unless function?
正如您在上面看到的,repeat
没有将任何内容传递给 unless()
。
实际上将 n
传递给 unless
.
body
中)
如果你只是想写一个函数来重复 x 次,并检查它是否是偶数,你可能想这样做:
function repeat(times) {
for (var i = 0; i < times; i++) {
output.innerHTML+= is_even(i)+"\n";
}
}
function is_even(n) {
if ( n % 2 ) return n+" is even";
return n+" is odd";
}
var output = document.getElementById('output');
repeat(6); // Output variable wasn't passed to this function, we're using it globally
<pre id='output'></pre>