What is the this keyword and how does it work?
Answer
Object literals
var myObject = {
property: this,
regularFunction: function() {
return this
},
arrowFunction: () => {
return this
},
iife: (function() {
return this
})()
}
myObject.regularFunction() // myObject
myObject["regularFunction"]() // my Object
myObject.property // NOT myObject; lexical `this`
myObject.arrowFunction() // NOT myObject; lexical `this`
myObject.iife // NOT myObject; lexical `this`
const regularFunction = myObject.regularFunction
regularFunction() // NOT myObject; lexical `this`Event listeners
Constructors
Manual
Unwanted this
thisGood to hear
Additional links
PreviousWhat does the following code evaluate to?NextWhat are landmark roles and how can they be useful?
Last updated