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
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
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 | 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
Scala | Method Invocation Method Invocation is a technique that demonstrates different syntax in which we dynamically call methods of a class with an object. The naming conventions of Scala are same as of Java, that are:- There should not be any space between the invocation object/target and the dot(.) nor a space between th
3 min read
Instance Methods in Java Instance Methods are the group of codes that performs a particular task. Sometimes the program grows in size, and we want to separate the logic of the main method from other methods. A method is a function written inside the class. Since java is an object-oriented programming language, we need to wr
5 min read
Scala | Functions Call-by-Name In Scala when arguments pass through call-by-value function it compute the passed-in expression's or arguments value once before calling the function . But a call-by-Name function in Scala calls the expression and recompute the passed-in expression's value every time it get accessed inside the funct
3 min read
Ruby - Method Invocation Method invocation refers to how a method is called in a program. The process of invoking a method in Ruby is quite easy since the use of parenthesis is optional. The use of parenthesis plays an important role when the invocation is embedded inside an expression consisting of other function calls or
3 min read
Argument vs Parameter in Java Argument An argument is a value passed to a function when the function is called. Whenever any function is called during the execution of the program there are some values passed with the function. These values are called arguments. An argument when passed with a function replaces with those variabl
2 min read
Recursion in Java In Java, Recursion is a process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. A few Java recursion examples are Towers of Hanoi (TOH)
6 min read