Reputation: 295
I am new to Javascript, and sturdying about the concept of higher order function, and using function as parameters.
I got two examples of code from online, and I do not understand what is going to happen.
First one, output will be 0, and I could not print the time of t1 and t2 ( I tried console.log(t1), but it became reference error). I wonder why it became 0. Also, I don't get how funcParameter() that is inside of functionbody is working for the function, though I know funcparameter() is callback function, and which is addOneToOne().
Second one, output will be 3. but how can it be 3 even though, I haven't put parameter into addTwo()? If I have not put anything in parameter, num will be automatically 0 or undefined?
I am sorry for many questions, but I really appreciate if you could help me out.
//1st code
const timeFuncRuntime = funcParameter => {
let t1 = Date.now();//
funcParameter();
let t2 = Date.now();//
return t2 - t1;
}
const addOneToOne = () => 1 + 1;
timeFuncRuntime(addOneToOne);
console.log(timeFuncRuntime(addOneToOne))//0
//2nd code
const addTwo = num => num + 2;
const checkConsistentOutput = (func, val) => {
let firsttry = func(val);
let secondtry = func(val);
if(firsttry === secondtry){
return firsttry;
} else {
return 'This function returned inconsistent results';
}
};
checkConsistentOutput(addTwo,1)
console.log(checkConsistentOutput(addTwo,1))//3
Upvotes: 1
Views: 956
Reputation: 1074198
First one, output will be 0, and I could not print the time of t1 and t2 ( I tried console.log(t1), but it became reference error). I wonder why it became 0.
Because zero milliseconds passed between the first call to the callback and the second. Date.now()
works in milliseconds. The call probably took microseconds at most.
You couldn't access t1
or t2
because they're local variables in the timeFuncRuntime
function, so you can't access those variables outside the timeFuncRuntime
function, only inside it.
Also, I don't get how funcParameter() that is inside of functionbody is working for the function, though I know funcparameter() is callback function, and which is addOneToOne().
Functions are objects in JavaScript. When you do timeFuncRuntime(addOneToOne)
, you're passing a reference to the function into timeFuncRuntime
which it receives as the value of the funcParameter
parameter. So when that code does funcParameter()
, it's calling addOneToOne
.
Second one, output will be 3. but how can it be 3 even though, I haven't put parameter into addTwo()?
Because checkConsistentOutput
did it. When you do console.log(checkConsistentOutput(addTwo,1))
, you're passing a reference to addTwo
and the number 1
into checkConsistentOutput
. It receives them as its func
and val
parameters. So when it does func(val)
, it's calling addTwo
with the value 1
. It gets the result in the firsttry
variable, then does the call again and gets the result in secondtry
, and since they match, it returns the value of firsttry
, which is 3
.
Upvotes: 1
Reputation: 10384
1: The scope of the t1
variable is limited to the function block. You can read more about variables scope here
2: You are using val
as function parameter, and the value of val
is 1.
1 + 2 = 3
The 1
is in this line:
checkConsistentOutput(addTwo,1)
Upvotes: 1
Reputation: 943220
Second one, output will be 3. but how can it be 3 even though, I haven't put parameter into addTwo()?
You copy the function stored in addTwo
as the first argument to checkConsistentOutput
where it gets assigned to func
. Then you call the function here — let firsttry = func(val);
— and here — let secondtry = func(val);
Upvotes: 0