return 如何在没有调用函数的情况下在 Javascript 中工作?
How does return work in Javascript without a function being invoked?
我正在研究这个 site 并想到了这个问题和答案:
Write a sum method which will work properly when invoked using either
syntax below.
console.log(sum(2,3)); // Outputs 5
console.log(sum(2)(3)); // Outputs 5
//答案
function sum(x) {
if (arguments.length == 2) {
return arguments[0] + arguments[1];
} else {
return function(y) { return x + y; };
}
}
我理解 if 语句中的代码,但不理解 else 语句中的代码,因为它只是 return 一个函数。该函数未使用“()”调用,因此考虑到 console.log(sum(2)(3));
的第二种情况,我不明白为什么会 return 5。我只能看到它会 return function(3) {return 2 + 3}
这应该会引发错误。
sum(2) = function (y){ return 2 + y }
您正在使用 (3)
调用该函数
您可以将函数存储在变量中。
基本思路是
var foo = function(y) {
return y
};
所以当我们看
console.log(sum(2)(3));
可以写成
var part1 = sum(2); //aka part1 = function(y) { return 2 + y; };
var ans = part1(3);
console.log(ans);
else
部分是接受 one
参数的 anonymous function。
万一sum(2)(3)
它进入 else 循环,其中 x = 2
因为它被称为 sum(2)
,而 (3)
是一个没有名字的匿名函数。
因此,
return function(y) { return x + y; };
变成
return function(3) { return 2 + 3; };
else 语句是 return使用第一次调用封装的 x 值的函数
解释一下就分解一下
var fresult = sum(3);
// right now fresult is a function that will add 3
// to the variable you call fresult with
// basically function(y){ return 3 + y; }
var result = fresult(4);
//result is 7
console.log(result);
可以把它想象成 x 的 值被 函数 捕获,而函数 正在被 returned,这然后你可以打电话
这个封装是由else的return语句中的匿名函数中创建的闭包创建的
要了解有关闭包的更多信息,请查看 this MDN Article about them,他们会比我解释得更好。
他们实际上有一个与你的非常相似的例子,他们试图将这个概念解释为类似于工厂:
function makeAdder(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
文章说:
In this example, we have defined a function makeAdder(x) which takes a
single argument x and returns a new function. The function it returns
takes a single argument y, and returns the sum of x and y.
In essence, makeAdder is a function factory — it creates functions
which can add a specific value to their argument. In the above example
we use our function factory to create two new functions — one that
adds 5 to its argument, and one that adds 10.
add5 and add10 are both closures. They share the same function body
definition, but store different environments. In add5's environment, x
is 5. As far as add10 is concerned, x is 10.
因此,如果您习惯于更传统的编程语言,那么私有变量的一些用例将与在闭包中捕获变量值相同
Javascript 与许多语言有点不同。函数可以作为变量传递。在您的情况下,当分解为实际发生的步骤时,会更容易看到正在发生的事情。
首先,由于 sum(2)
只有一个参数,我们转到您已经知道的 else
块。那里发生的事情是我们正在返回一个函数,用 x
代替传递给 sum(x)
的变量。所以基本上,返回的是 returns 2 + y
的函数。如果我们想自己写出来,这就是它的样子:
function(y) {
return 2 + y;
}
sum(2)(3)
中的第二组括号基本上是在说“调用 sum(2)
返回的函数并向其发送参数 3
.
这里基本上是整个操作的扩展版本:
function sum(x) {
if (arguments.length == 2) {
return arguments[0] + arguments[1];
} else {
return function(y) { return x + y; };
}
}
var addTwo = sum(2);
console.log(addTwo(3));
简短版本基本上只是跳过为 sum(2)
的结果创建一个单独的变量,而是立即调用新函数。
我正在研究这个 site 并想到了这个问题和答案:
Write a sum method which will work properly when invoked using either syntax below.
console.log(sum(2,3)); // Outputs 5
console.log(sum(2)(3)); // Outputs 5
//答案
function sum(x) {
if (arguments.length == 2) {
return arguments[0] + arguments[1];
} else {
return function(y) { return x + y; };
}
}
我理解 if 语句中的代码,但不理解 else 语句中的代码,因为它只是 return 一个函数。该函数未使用“()”调用,因此考虑到 console.log(sum(2)(3));
的第二种情况,我不明白为什么会 return 5。我只能看到它会 return function(3) {return 2 + 3}
这应该会引发错误。
sum(2) = function (y){ return 2 + y }
您正在使用 (3)
您可以将函数存储在变量中。
基本思路是
var foo = function(y) {
return y
};
所以当我们看
console.log(sum(2)(3));
可以写成
var part1 = sum(2); //aka part1 = function(y) { return 2 + y; };
var ans = part1(3);
console.log(ans);
else
部分是接受 one
参数的 anonymous function。
万一sum(2)(3)
它进入 else 循环,其中 x = 2
因为它被称为 sum(2)
,而 (3)
是一个没有名字的匿名函数。
因此,
return function(y) { return x + y; };
变成
return function(3) { return 2 + 3; };
else 语句是 return使用第一次调用封装的 x 值的函数
解释一下就分解一下
var fresult = sum(3);
// right now fresult is a function that will add 3
// to the variable you call fresult with
// basically function(y){ return 3 + y; }
var result = fresult(4);
//result is 7
console.log(result);
可以把它想象成 x 的 值被 函数 捕获,而函数 正在被 returned,这然后你可以打电话
这个封装是由else的return语句中的匿名函数中创建的闭包创建的
要了解有关闭包的更多信息,请查看 this MDN Article about them,他们会比我解释得更好。
他们实际上有一个与你的非常相似的例子,他们试图将这个概念解释为类似于工厂:
function makeAdder(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
文章说:
In this example, we have defined a function makeAdder(x) which takes a single argument x and returns a new function. The function it returns takes a single argument y, and returns the sum of x and y.
In essence, makeAdder is a function factory — it creates functions which can add a specific value to their argument. In the above example we use our function factory to create two new functions — one that adds 5 to its argument, and one that adds 10.
add5 and add10 are both closures. They share the same function body definition, but store different environments. In add5's environment, x is 5. As far as add10 is concerned, x is 10.
因此,如果您习惯于更传统的编程语言,那么私有变量的一些用例将与在闭包中捕获变量值相同
Javascript 与许多语言有点不同。函数可以作为变量传递。在您的情况下,当分解为实际发生的步骤时,会更容易看到正在发生的事情。
首先,由于 sum(2)
只有一个参数,我们转到您已经知道的 else
块。那里发生的事情是我们正在返回一个函数,用 x
代替传递给 sum(x)
的变量。所以基本上,返回的是 returns 2 + y
的函数。如果我们想自己写出来,这就是它的样子:
function(y) {
return 2 + y;
}
sum(2)(3)
中的第二组括号基本上是在说“调用 sum(2)
返回的函数并向其发送参数 3
.
这里基本上是整个操作的扩展版本:
function sum(x) {
if (arguments.length == 2) {
return arguments[0] + arguments[1];
} else {
return function(y) { return x + y; };
}
}
var addTwo = sum(2);
console.log(addTwo(3));
简短版本基本上只是跳过为 sum(2)
的结果创建一个单独的变量,而是立即调用新函数。