+ 2
var r = (function (x, f = () => x) { var x; var y=x; x = 2; return (x + y + f()); })(1) console.log(r);
Can you explain why r = 4
4 Antworten
+ 4
oh so that f() is counted as 1 .
Sorry that I guessed that wrong .
+ 2
first you passed a parameter to the function which has value = 1.
You have stored it in x
x = 1
then you stored it in y
y = x
as x = 1
so
y = 1
then you changed the value of x to 2
x = 2
in return a value you returned
x + y + f()
as f contains no parameter , so it will be zero hence it will return x + y
= 1 + 2
= 3
+ 2
first you passed a parameter to the function which has value = 1.
You have stored it in x
x = 1
then you stored it in y
y = x
as x = 1
so
y = 1
then you changed the value of x to 2
x = 2
in return a value you returned
x + y + f()
as f contains no parameter , so it will be zero hence it will return x + y
= 1 + 2
= 3
and f()=1
then x + y + f() = 1+2+1 = 4
example f()=2
It will be 2 + 2 +2 = 6
f() = 3
3 + 2 + 3 = 8
- 1
but result is r = 4