Article From:https://segmentfault.com/q/1010000011699122
Question:

Look at the following function first.

``````function createMathOperation(operator, defaultValue) {
return (value, other) => { // It's a fantastic way to write, createMathOperation calls the operator's parameters directly, and how does it do it without any reference?Let resultIf (value = = = u)Ndefined & & other = = = undefined) {/ / if two parameters do not exist, return defaultValueReturn defaultValue}If (value = = undefined) {/ / if there is value, return value.Result = value}If (other! = =)Undefined) {/ / if there is otherIf (result == undefined) {// if there is another and value does not exist (why not write value === here)Undefined? It looks the same, and value understands better) and returns to other.Return other}If (typeof value = ='stri)Ng'typeof other = ='string') {/ / if there are strings in the two parameterValue = baseToString (value)Other =BaseToString (other)} else {Value = baseToNumber (value)Other = baseToNumber(other)}Result = operator (value, other)}Return result}}Const add = creAteMathOperation ((augend, addend) => augend + addend, 0);Add (4,6); / / --> 10``````

My own guess is that operator (value, other) is the relationship of the scope chain that accesses the value and other of the arrow function, but most
The magic is when it calls:

This add (4, 6); what happened? 4 and 6`createMathOperation(){}` The parameters can be understood and how to be.`operator(){}`Got it?
Beg the great GodPointing out the truth and spreading doubtsah

In fact, it is a classic closure. What you call operator is actually (augend, addend) => augend + addend is function. Then you say your 4 and 6 are not createMathOpe.Ration got it, but function came out of return.

``````function createMathOperation(operator, defaultValue){
return function(value, other){
// addIn fact, this is what we return.}}``````

It’s a closure.

``````function func（cb）{
return function（value，value2）{
//Here we can make some judgments about the parameters.Return CB (value, Value2)}}Var a = func (function (value, Value2) {Return value+vaLue2;})A (4, 6) //10``````

It is not necessary to write so complex that it is difficult for people to understand.
It should be part of the calculator program, which is to perform an addition. If all incoming numbers are returned, the sum of the two numbers is returned. If a string is passed, the spliced string is returned.

4,6Not as`createMathOperation`The parameters are passed in.`createMathOperation`The parameter is a function and a default value.`(augend, addend) => augend + addend`And 0.
add(4,6)In fact, 4 and 6 were taken as actions.`createMathOperation`returnThe parameters of the function are relatively difficult to understand. Then all your doubts were unraveled.

createMathOperation The return value is a closure function. Operator passes in a function, which is executed at that time.

This is the order of execution.

createMathOperation(<Anonymous Function 1>, 0) -> return <Anonymous Function 2>

Because both of the add parameters are numbers, it simply skips the content and executes directly & lt; Anonymous Function 1 & gt; – Operator

add(4,6) -> return <Anonymous Function 2>(4, 6)

So the result is 10.