Phone Screen Facebook

var length = 10;
function fn() {
console.log(this.length);
}

var obj = {
  length: 5,
  method: function(fn) {
    fn();
    arguments[0]();
  }
};

obj.method(fn, 1);

what is the result ?

In non-strict mode:
10
2

What happens:

  • var gets attached to the global object.
  • Without the call method, the this value inside of fn will refer to the global object.
  • This will refer to the global object, thus will ref var length = 10;
  • For arguments0, this refers to the arguments object, which has a length of 2

In strict mode:
throw error
2