Recently one of my friends asked me the output of following code
var length = 10;
function fn() {
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
}
};
obj.method(fn, 1);
I thought the answer would be 10 10 but surprisingly for second call i.e. arguments[0]();
the value comes out to be 2 which is length of the arguments passed.
In other words it seems arguments[0]();
has been converted into fn.call(arguments);
.
Why this behavior? Is there a link/resource for such a behavior?