JS Dasar: 58. Kata kunci this
by istiq20
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Kata kunci this</title>
</head>
<body>
<script>
// this di global scope
console.info(this); // window
// this di function
function sample() {
console.info(this); // window
function inner() {
console.info(this); // window
}
inner();
}
sample();
// this di Object Method
const person = {
name: "Eko",
sayHello: function (value) {
// this = person
console.info(`Hello ${value} My Name is ${this.name}`);
},
};
person.sayHello("Budi"); // Hello Budi My Name is Eko
person.sayHello("Joko"); // Hello Joko My Name is Eko
</script>
</body>
</html>