Callback in ES6
JavaScript
class Child {
constructor(parentFunction){
this.parentFunction = parentFunction;
}
update(){
console.log('child update');
this.parentFunction();
}
}
class Parent {
constructor(){
// whereever the child gets create, pass in the parent's callback function
this.child = new Child(this.update);
}
update(){
console.log('parent update');
}
getChild(){
return this.child;
}
}
var p = new Parent();
var c = p.getChild();
c.update();