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 6createMathOperation(){} The parameters can be understood and how to be.operator(){}Got it?
Beg the great GodPointing out the truth and spreading doubtsah

Answer 0:

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.}}

Answer 1:

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

Answer 2:

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.
add(4,6) // 10
add(“a”,1) // “a1”

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

Answer 3:

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>

So now add = < Anonymous Function 2> = (augend, addend) => augend + addend.

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.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *