JavaScript this keyword
web development Daq JavaScript programming Hol law' lo'lu'. 'ach concepts Qatlh ghaj, developers Qatlh. wa' "this" keyword ghaH.
JavaScript "this" keyword yajbe' (Hol pIm vo' developers). "this" context function Qu'DI' ghaH. 'ach context choH 'ej results Qapla' pagh ghaj.
misvam yIlaD:
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
console.log(this.firstName + " " + this.lastName);
}
}
person.fullName(); // Output: John Doe
misvam Daq "this" "person" object tlhap. "fullName" function "person" context Daq Qu'.
'ach function context pIm Daq Qu'DI' nuq ghaH? yIlaD:
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
console.log(this.firstName + " " + this.lastName);
}
}
const printFullName = person.fullName;
printFullName(); // Output: undefined undefined
"fullName" function global context Daq Qu'. global context Daq "firstName" "lastName" pagh vaj "undefined" nob.
Qagh Qotlh: "bind" method lo' "this" "person" object Daq jan:
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
console.log(this.firstName + " " + this.lastName);
}
}
const printFullName = person.fullName.bind(person);
printFullName(); // Output: John Doe
"bind" method lo' "this" "person" object Daq jan, function Daq Qu'.
JavaScript "this" keyword yajbe'. 'ach Qu' 'ej yaj vaj developers Qagh Qotlh. "bind" method lo' vaj function reH context Dun Daq Qu'.