0

imagine having this object :

var a = {
   b : () => console.log("yo"),
   c : () => b()
}

How can I execute the b function calling the c one ? the syntax above doesn't work... Do you have an idea ?

Shivam
  • 3,514
  • 2
  • 13
  • 27
patacoing
  • 87
  • 1
  • 10

4 Answers4

1
var a = {
   b : () => console.log("yo"),
   c : () => a.b() // reference to the object
}

or

var a = {
   b : () => console.log("yo"),
   c : function () {return this.b()} // use function syntax instead of lambda to gain a reference to the object on which c() is called
}
Mihályi Zoltán
  • 822
  • 6
  • 11
0

Maybe this works?

const a = {
  b : () => {console.log("yo")},
  c : () => {a.b()}
}

a.c()
ask4you
  • 768
  • 3
  • 14
0

Instead of arrow functions use non-arrow functions, and then this will be available, if the method is called in the usual way:

var a = {
   b() { console.log("yo") },
   c() { return this.b() }
}

a.c();
trincot
  • 317,000
  • 35
  • 244
  • 286
0

make an actual class:

function A() {
  this.b = () => console.log('yo');
  this.c = () => this.b()
}

var a = new A();

a.c();
bryan60
  • 28,215
  • 4
  • 48
  • 65