According to the v8 ticket, it said
Tail call elimination isn’t compatible with JavaScript as it is used in the real
world. Consider the following:
function foo(x) {
return bar(x + 1);
}
function bar(x) {
return foo.arguments[0];
}
foo(1)
This returns 1.
It didn’t explain clearly what if JavaScript support tail call, what would be the value of foo(1) and why?
Anyone can explain?
The value will be
1when you dofoo(1)becausefoofunction returns the result ofbarfunction andbarfunction does nothing but read the first argument offoofunction withfoo.arguments[0](argumentsis implied object available to every function which is used to read arguments passed to function) and return it. The first argument offoohappens to be1when you do:Here is break-down:
The
barfunction just reads first argument offoo(viafoo.arguments[0]) and returns it because of which no addition is done.