apply() metodi
apply() metodi bilan biz metod yoza olamiz, qaysiki turli obyektlarda ishlatilina oladigan
apply() metodi call() metodiga o'xshashdir.
Asosiy farqi apply() metodida argumentlar array sifatida beriladi. call() metodida esa argumentlar birma bir beriladi.
const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
Yana boshqa misol
function introduce(language, country) {
console.log(`${this.name} speaks ${language} and lives in ${country}`);
}
const person = { name: "Alice" };
introduce.apply(person, ["English", "Canada"]);
// Output: Alice speaks English and lives in Canada
Misol:
function greet(title, city) {
console.log(`${title} ${this.name} from ${city}`);
}
👉 Use apply() to make the output look like this:
Mr. David from New York
✅ Explanation:
"Run thegreetfunction, and while doing that, makethisinsidegreetpoint touser."
✅ The function (greet) is using the object (user) as its this context.
In short: Function uses the object, not the other way around.
🔁 To rephrase your question:
❌ “Does the object use the function?”
✅ No — the function is using the object as itsthis.
🔍 Analogy:
You're borrowing someone’s clothes (function borrowing object’s properties),
not the clothes borrowing you 😄