What is THIS in JavaScript?

7 Views
Published
#JavaScript #tutorial #course

// this = reference to the object where THIS is used
// (the object depends on the immediate context)

const person1 = {
name: "Spongebob",
favFood: "hamburgers",
sayHello: function(){console.log(`Hi! I am ${this.favFood}`)},
eat: function(){console.log(`${this.name} is eating ${this.favFood}`)}
}
const person2 = {
name: "Patrick",
favFood: "pizza",
sayHello: function(){console.log(`Hi! I am ${this.favFood}`)},
eat: function(){console.log(`${this.name} is eating ${this.favFood}`)}
}

person1.eat();
person2.eat();
Category
Bro Code
Tags
javascript, javascript tutorial, javascript full course
Be the first to comment