0
function somefunction(cb) {
          for(var i=0; i<100000; i++){
          }
          cb.call({});
};

var a=10;
function test(params) {
    somefunction(function () {
      console.log(this.a);
    });

    somefunction(()=>{
      console.log(this.a);
    });
};

test();

output of the above code is undefined & 10

I am curious to know when does context binding happened for the arrow function when it printed 10 in the second somefunction call even though i specified different context through bind.

Amar T
  • 359
  • 1
  • 7

1 Answers1

0

Refer http://www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions-runtime-semantics-evaluation In arrow functions, context gets inherited by default, whereas traditional function we have to use .bind(this)

    var a=10;
    function test(params) {
      somefunction(function () {
        console.log(this.a);
      }.bind(this));

      somefunction(()=>{
        console.log(this.a);
      });
    };

   test();
Shashank
  • 830
  • 1
  • 6
  • 14