JavaScript嵌套函数式计算代码分步工作原理及实现细节问询
Hey there! Let's break down this clever functional programming code piece by piece so you can fully grasp how it works, and even write similar code yourself later.
1. 核心函数 n:数字的"包装器"
First up is the foundational function n:
var n = function(digit) { return function(op) { return op ? op(digit) : digit; } };
Let's unpack this line by line:
nis a higher-order function (a function that returns another function). When you pass a number like0or1ton, it returns a new inner function.- That inner function takes one argument:
op(short for "operation").- If you pass a function to
op(like an addition or multiplication function), it will runop(digit)—meaning it feeds the original digit into that operation. - If you don't pass anything to
op(like when you calltwo()), it just returns the original digit directly.
- If you pass a function to
2. 数字变量(zero到nine):都是"待命"的函数
When we create variables like var one = n(1);, we're not storing the number 1 directly—we're storing the inner function returned by n(1).
So:
- Call
one()with no arguments, and it returns the number 1 (sinceopis undefined, the inner function falls back to returningdigit). - Call
one(someOperation), and it will pass 1 into that operation function and return the result.
3. 运算符函数:先"抓"右边的数,等左边的数
Take the plus function as an example:
function plus(r) { return function(l) { return l + r; }; }
This is another higher-order function, and it's key to how the math works:
- When you call
plus(two()), firsttwo()returns 2, soplus(2)runs. plus(2)returns a new function that's waiting for a left-hand numberl. When it gets that number, it calculatesl + 2and returns the result.
The same logic applies to all operators:
minus(r)returns a function that subtractsrfrom whatever left number comes in.times(r)returns a function that multiplies the left number byr.dividedBy(r)returns a function that divides the left number byr.
4. 示例拆解:跟着代码走一遍
Let's take one(plus(two()))—the first example—and walk through every step of its execution:
- Execute
two(): Sincetwois the inner function fromn(2), and we're calling it with noopargument, it returns the number 2. - Pass 2 to
plus:plus(2)runs, and returns a new function:function(l) { return l + 2; }. - Pass that function to
one:oneis the inner function fromn(1). Nowopis defined (it's the addition function we just made), so it runsop(1)—which is1 + 2—and returns 3.
Another example: seven(times(five()))
five()returns 5.times(5)returnsfunction(l) { return l * 5; }.sevenreceives this function, runsop(7)→7 * 5 = 35, returns 35.
5. 核心逻辑总结
This code uses a technique called currying—breaking down multi-argument operations into a sequence of single-argument function calls. It makes the code read almost like plain English ("one plus two" instead of 1 + 2).
Each number acts as a dual-purpose function:
- Call it alone (e.g.,
three()) to get the raw number. - Pass an operator to it (e.g.,
three(plus(four()))) to trigger the operation with that number as the left-hand value.
内容的提问来源于stack exchange,提问作者HakunaDio

