- Published on
Understanding 'this' in JavaScript
191 words1 min read
- Authors
- Name
- Curtis Warcup
this
What is 'this' used for in a class?
- Is used to refer back to the instance of a class.
How is the value of 'this' determined?
class Car {
setDriveSound(sound) {
this.sound = sound
}
drive() {
return this.sound
}
}
const car = new Car()
car.setDriveSound('Vroom')
car.drive() //'vroom' gets returned
When we call the drive function..
drive() {
return this.sound
}
This refers to the car
. To the LEFT of the this
keyword