+ 1
pls tell me how the code work, especially this sentence: val=(arg[i]).call(window,val);đ§đ§đ§array looks like a function here
function pipe(...arg){ let len=arg.length; let val=arg[0]; for(let i=1;i<len;i++){ val=(arg[i]).call(window,val); return val; }} pipe("5a0",parseInt,console.log);
4 Answers
+ 4
The pipe function takes multiple arguments, the first one is supposed to be a value, and the rest should be functions which process the value in turn, and pass the result to the next function.
arg is an array (because of the ... spread operator all argument of pipe are stored in this array).
The for loop goes through the function arguments (starting from index 1).
arg[i].call(window, val)
Here the function is invoked.
Its first parameter "window" is the context where the function is executed, then val is the argument.
so function call for the first loop iteration will translate to this:
window.parseInt("5a0")
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
+ 3
Just letting you know, the for...loop.was missing a closing curly bracket for its body, after the line where <val> was assigned new value.
Also you have a typo in `parseInt`, you should be using uppercase letter i not lowercase letter L
parseInt -> 'parse' string into 'Int'eger
+ 2
Tibor Santa really thanksđđ»
+ 1
Ipang thanks for correctingđ