Daftar Isi
Keyword this adalah object pemilik suatu baris code yang sedang dieksekusi.
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
this Dalam Fungsi Object #
Keyword this adalah object pemilik fungsi dalam suatu object. Pada contoh di bawah ini, this adalah object person. Object person adalah pemilik fungsi fullName.
fullName : function() {
return this.firstName + " " + this.lastName;
}
this Sebagai Global Variable #
Keyword this adalah global variable window apabila dipanggil sebagai global variable.
var x = this;
Keyword this juga adalah global variable window apabila dipanggil dalam fungsi yang bukan merupakan fungsi dalam object.
function myFunction() {
return this;
}
this Dalam Atribut Event #
Keyword this adalah elemen HTML yang menerima event apabila dipanggil dalam atribut event.
<button onclick="this.style.display='none'">
Click to Remove Me!
</button>
this Dalam Fungsi Object #
Keyword this adalah object pemilik baris coding yang sedang dieksekusi apabila dipanggil dalam suatu object.
Pada dua contoh di bawah ini, this adalah object person.
var person = {
firstName : "John",
lastName : "Doe",
id : 5566,
myFunction : function() {
return this;
}
};
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};