JavaScript 📜
October 1

call() methodi

Barcha funksiyalar metodlardir.

Javascriptda barcha funksiyalar obyekt metodlaridir.

Agar funksiya biror obyektning metodi bo'lmasa u global obyektining funksiyasi bo'ladi.

Quyidagi funksiya 3 ta xususiyatga ega bo'lgan obyekt yaratadi.

const person = { firstName:"John", lastName: "Doe", fullName: function () { return this.firstName + " " + this.lastName; } }

// This will return "John Doe": person.fullName();

call() metodi

functionName.call(thisArg, arg1, arg2, ...)

call() metodi Javascriptning oldindan belgilangan metodidir

It can be used to invoke (call) a method with an object as an argument (parameter).

📝 Note

call() metodi bilan biror obyekt boshqa obyektga tegishli bo'lgan metodn ishga tushirish mumkin

Quyidagi misol person'ning fullName metodini chaqiradi uni person1'da ishlatgan holda:

const person = { fullName: function() { return this.firstName + " " + this.lastName; } } const person1 = { firstName:"John", lastName: "Doe" } const person2 = { firstName:"Mary", lastName: "Doe" }

// This will return "John Doe": person.fullName.call(person1);

Metodlarni (qarzga) olish

const person1 = { firstName: "John", lastName: "Doe", };

const person2 = { firstName: "Alice", lastName: "Smith", };

function greet(message) { console.log(`${message}, my name is ${this.firstName} ${this.lastName}`); }

// Normally calling greet.call(person1, "Hello"); // Hello, my name is John Doe greet.call(person2, "Hi"); // Hi, my name is Alice Smith

Bu erda call()funktsiyani (greet funksiyasi nazarda tutilyapti) person1yoki person2 obyektlarini o'zining this'i sifatida ishlashiga majbur qiladi.

Challenge 1: Method Borrowing
👉 Borrow the introduce method from teacher and use it with student.
const teacher = { name: "Mr. Smith", subject: "Math", introduce: function() { console.log(`Hello, I am ${this.name} and I teach ${this.subject}`); } }; const student = { name: "Alice", subject: "Physics" }; teacher.introduce.call(student);


Challenge 2: Greeting Function
Use call() to greet John with 'Hello!' and Maria with 'Hi?'.


function greet(greeting, punctuation) { console.log(`${greeting}, ${this.name}${punctuation}`); } const person1 = { name: "John" }; const person2 = { name: "Maria" }; greet.call(person1, "Hello", "!"); greet.call(person2, "Hi", "?");


Challenge 3: Calculator with call()
Make sum1 and sum2 print their results with labels using call().


const calculator = { add: function(a, b) { console.log(`${this.label}: ${a + b}`); } }; const sum1 = { label: "Sum1" }; const sum2 = { label: "Sum2" }; calculator.add.call(sum1, 5, 10); calculator.add.call(sum2, 20, 30);

Challenge 4: Reuse a Function
Use call() to describe jobs of David and Emma with different ages and cities.


function describeJob(age, city) { console.log(`${this.name} is ${age} years old and works as a ${this.job} in ${city}`); } const personA = { name: "David", job: "Developer" }; const personB = { name: "Emma", job: "Designer" }; describeJob.call(personA, 37, "Fergana"); describeJob.call(personB, 33, "New York");


Challenge 5: call() Inside Loop
Use call() inside a loop to print Sophia's skills.


function printInfo(skill) { console.log(`${this.name} knows ${skill}`); } const user = { name: "Sophia" }; const skills = ["JavaScript", "React", "Node.js"]; for (let skill of skills) { printInfo.call(user, skill);

}