JavaScript Function Invocation
Last Updated :
17 Dec, 2024
In JavaScript, function invocation refers to executing the code defined in a function. Function invocation occurs when a function is called or executed in JavaScript. When invoked, the code inside the function block is run, and any return value is computed and passed back to the caller.
JavaScript
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Meeta"));
- The function greet is invoked with the argument "Meeta".
- The string "Hello, Meeta!" is returned and logged to the console.
Types of Function Invocation
JavaScript provides several ways to invoke functions. Each method affects the behavior of this (the execution context) and other factors.
1. Function Invocation
When a function is called directly using its name, it operates in the global or local scope.
JavaScript
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
Scope: In non-strict mode, this defaults to the global object (window in browsers).
2. Method Invocation
When a function is a property of an object and is invoked as object.method(), it is called a method invocation.
JavaScript
const user = {
name: "Meeta",
greet: function () {
return `Hello, ${this.name}!`;
},
};
console.log(user.greet());
In method invocation, this refers to the object that owns the method (in this case, user).
3. Constructor Invocation
Functions can be invoked as constructors using the new keyword. When invoked this way, the function creates a new object and sets this to refer to that object.
JavaScript
function Person(name, age) {
this.name = name;
this.age = age;
}
const meeta = new Person("Meeta", 25);
console.log(meeta.name);
A constructor invocation returns the newly created object.
4. Indirect Invocation
Functions can be invoked indirectly using call(), apply(), or bind().
call(): Invokes a function and explicitly sets this and individual arguments.
JavaScript
function greet(greeting) {
return `${greeting}, ${this.name}!`;
}
const user = { name: "Meeta" };
console.log(greet.call(user, "Hello"));
apply(): Similar to call(), but arguments are passed as an array.
JavaScript
function greet(greeting) {
return `${greeting}, ${this.name}!`;
}
const user = { name: "Meeta" };
console.log(greet.apply(user, ["Hi"]));
bind(): Creates a new function with this permanently set to the provided value.
JavaScript
function greet(greeting) {
return `${greeting}, ${this.name}!`;
}
const user = { name: "Meeta" };
const boundGreet = greet.bind(user);
console.log(boundGreet("Hey"));
5. Self-Invoking Functions
Self-invoking (or immediately invoked) functions run automatically without being explicitly called. These are often written as Immediately Invoked Function Expressions (IIFEs).
JavaScript
(function () {
console.log("This is a self-invoking function!");
})();
OutputThis is a self-invoking function!
IIFEs are commonly used for encapsulating code to avoid polluting the global scope.
6. Arrow Function Invocation
Arrow functions are invoked like regular functions but differ in how they handle this. They do not bind their own this; instead, they inherit this from their lexical scope.
JavaScript
const user = {
name: "Meeta",
greet: () => {
return `Hello, ${this.name}!`;
// `this` here is not bound to `user`
},
};
console.log(user.greet());
Similar Reads
JavaScript Function Definitions
JavaScript functions are declared using the function keyword, either as a declaration or expression. Declarations define named functions, while expressions assign functions to variables. Both enable code reuse and modularity.SyntaxFunction Declarationsfunction functionName( parameters ) { // Stateme
2 min read
Explain invoking function in JavaScript
In this article, we will learn about invoking the function in Javascript, along with understanding its implementation through examples. Function Invoking is a process to execute the code inside the function when some argument is passed to invoke it. You can invoke a function multiple times by declar
2 min read
JavaScript Function() Constructor
The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
2 min read
JavaScript Anonymous Functions
An anonymous function is simply a function that does not have a name. Unlike named functions, which are declared with a name for easy reference, anonymous functions are usually created for specific tasks and are often assigned to variables or used as arguments for other functions.In JavaScript, you
3 min read
JavaScript Function Call
The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
2 min read
PHP | ReflectionFunction invoke() Function
The ReflectionFunction::invoke() function is an inbuilt function in PHP which is used to return the result of the invoked function call. Syntax: mixed ReflectionFunction::invoke( mixed $args) Parameters: This function accept parameter $args which holds the list of arguments passed to the called func
2 min read
PHP | ReflectionMethod invoke() Function
The ReflectionMethod::invoke() function is an inbuilt function in PHP which is used to invoke the specified reflected method and returns the result of the method.Syntax: public mixed ReflectionMethod::invoke ( $object, $parameter ) Parameters: This function accepts two parameters which are illustrat
2 min read
PHP | ReflectionFunction invokeArgs() Function
The ReflectionFunction::invokeArgs() function is an inbuilt function in PHP which is used to return the result of the invoked function call. Syntax: mixed ReflectionFunction::invokeArgs( array $args ) Parameters: This function accepts a single parameter $args which holds the array of arguments passe
2 min read
Functions in Programming
Functions in programming are modular units of code designed to perform specific tasks. They encapsulate a set of instructions, allowing for code reuse and organization. In this article, we will discuss about basics of function, its importance different types of functions, etc.Functions in Programmin
14 min read
JavaScript Function.prototype.call() Method
The call() method allows function calls belonging to one object to be assigned and it is called for a different object. It provides a new value of this to the function. The call() method allows you to write a method once and allows it for inheritance in another object, without rewriting the method f
3 min read