Last active
October 9, 2018 19:24
-
-
Save burnjohn/507062380dc326efbc629f8a5ac4fc43 to your computer and use it in GitHub Desktop.
Execution context example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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