Js one question, let's come in and have a look.

function add can achieve continuous addition operations
function add syntax is as follows
add (num1) (num2) (num3).; / / Note here is the ellipsis yo, infinite
use examples as follows:
add (10) (10) (10) = 20;
add (10) (20) (50) = 80;
add (10) (20) (50) = 180;
Please use js code to achieve the function add.

function add(num){
    var sum=num,
    
        tmp=function(v){
            sum+=v;
            return tmp
        };
    
    tmp.toString=function(){
        return sum
    };
    
    return tmp
}


console.log( add(10)(20)(50) )        //80

what is the operation that this tmp returns tmp? is this the legendary partial function? And the call to toString?

Apr.08,2021

console.log( add(10)(20)(50) )
The process that the code runs after

is:
1. First execute add (10), initialize the internal sum of the add function to 10,

return tmp=function(v){
            10+=v;
            return tmp
        };

2. Then execute tmp (20), that is,

return tmp=function(20){
            10+=20;
            return tmp
        };

3. Then execute tmp (50)

return tmp=function(50){
            30+=50;
            return tmp
        };

4. A type conversion function that is overridden is executed on subsequent output:

tmp.toString=function(){
        return sum
    };

5. Output the tmp function and the content, that is, the sum:80 at this time


call the function to return the function and it can go on all the time. Because the function is returned, the modified toString makes it impossible to print the number.


this is a href=. You can take a look at this Coriarization


var a=add(10);
console.log(a); // 10
var b=a(10);
console.log(b); // 20
console.log(a); // 20

is there nothing wrong with your code?
and closures for more information

< hr >

if you don't think or study, you will step on it.
ramdajs

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eaf60f-224f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eaf60f-224f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?