1

How can i extract the inner id? I know that it's possible to change the fat arrow function to standart function or instead of this.id to use obj.id. But is there any other way to get the inner id while using the fat arrow function?

var obj = {
  id: 1,
  cool: () => {
    console.log( this.id );
  }
};
var id = 2;
obj.cool(); // 2
Arkadiy Stepanov
  • 161
  • 1
  • 2
  • 12
  • Possible duplicate of [Methods in ES6 objects: using arrow functions](https://stackoverflow.com/questions/31095710/methods-in-es6-objects-using-arrow-functions) – Nguyen You Sep 26 '18 at 02:48

1 Answers1

0

Arrow function does not create a new scope and uses parent constructor's scope. in this case parent is Windows constructor Function so this refers to window and there is no way except the ones you listed

var obj = {
  id: 1,
  cool() {
    console.log( this.id );
  }
};
  • That is not the proper syntax. It would need to be `obj.cool.call(obj)`. But even like that its not working. – Arkadiy Stepanov Sep 26 '18 at 01:31
  • arrow function does not create a new scope and uses parent constructor's scope. in this case parent is Windows constructor Function so this refers to window and I guess there is no way except the ones you listed – Salih Şenol Çakarcı Sep 26 '18 at 01:46