this_in_JavaScript
this_in_JavaScript
JavaScript
this in JavaScript
A Comprehensive Guide to Mastering the
'this' Keyword
Understanding this
'this' in JavaScript refers to the execution context. It can be tricky!
Imagine a classroom. 'this' is like a student raising their hand, and
the teacher knows who's asking. In JavaScript, 'this' depends on
where and how a function is called.
example.js
const student = {
name: "Mike",
introduce: function () {
},
};
example.js
function myFunction() {
myFunction();
example.js
const car = {
brand: "Tesla",
start: function () {
},
};
example.js
const person = {
name: "Bob",
sayHi: () => {
},
};
example.html
<script>
button.addEventListener("click", function() {
});
</script>
example.js
const person = {
name: "Alice",
};
function sayHello(greeting) {
example.js
function Dog(name) {
this.name = name;
Gaurav Tikekar