Skip to content

Instantly share code, notes, and snippets.

@burnjohn
Last active October 9, 2018 19:24
Show Gist options
  • Save burnjohn/507062380dc326efbc629f8a5ac4fc43 to your computer and use it in GitHub Desktop.
Save burnjohn/507062380dc326efbc629f8a5ac4fc43 to your computer and use it in GitHub Desktop.
Execution context example
const person = {
name: 'Ivan',
age: 24,
showInfo() {
console.log('Info is: ', this.name, this.age)
}
};
const person2 = {
name: 'Anna',
age: 20,
showInfo: person.showInfo //context is not fixed
};
const person3 = {
name: 'Anna',
age: 20,
showInfo: person.showInfo.bind(person) //context is fixed
};
const person4 = {
name: 'Anna',
age: 20,
showInfo: () => {
person.showInfo() //context is fixed
}
};
person2.showInfo(); //context of method is person2
person3.showInfo(); //context of method is person
person4.showInfo(); //context of method is person
const testObj = {
data: 'data',
runCallback(cb){
console.log('This in callback ', this);
cb();
}
};
const testObj2 = {
data: 'data . 1111',
showThis(){
console.log('ob1 ', this);
}
};
testObj.runCallback(testObj2.showThis);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment