Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Function Call in JavaScript

Function Call in JavaScript

? Function.prototype.call() in JavaScript


What is call()?

  • call() is a method that invokes a function immediately, allowing you to specify the value of this inside that function.

  • It also lets you pass arguments one by one.


Syntax

func.call(thisArg, arg1, arg2, ...);
  • thisArg: The value to use as this inside the function.

  • arg1, arg2, ...: Arguments passed individually to the function.


Example 1: Basic Usage

function greet(greeting, punctuation) {  console.log(greeting + ", " + this.name + punctuation);}const person = { name: "Alice" };greet.call(person, "Hello", "!");  // Output: Hello, Alice!

Example 2: Using call() to Borrow Methods

const person1 = {  fullName: function() {    return this.firstName + " " + this.lastName;  }};const person2 = {  firstName: "John",  lastName: "Doe"};// Borrow fullName method from person1console.log(person1.fullName.call(person2));  // Output: John Doe

Difference Between call() and apply()

MethodArguments FormatCalls Immediately?
call()Comma-separated argumentsYes
apply()Array or array-likeYes

Summary

Featurecall()
Calls functionYes
Sets thisYes
ArgumentsPassed one by one
Returns resultYes

Want me to show how to use call() for function borrowing or variadic functions?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql