JavaScript Apply() Function (original) (raw)
Last Updated : 26 May, 2023
The apply() method is used to write methods, which can be used on different objects. It is different from the function call() because it takes arguments as an array.
Syntax:
apply()
Return Value: It returns the method values of a given function.
Example 1: This example illustrates the apply() function without arguments.
JavaScript `
let student = { details: function () { return this.name + this.class; } } let stud1 = { name: "Dinesh", class: "11th", } let stud2 = { name: "Vaibhav", class: "11th", }
let x = student.details.apply(stud2); console.log(x);
`
Output:
Vaibhav 11th
Example 2: This example illustrates the apply() function with arguments.
JavaScript `
let student = { details: function (section, rollnum) { return this.name + this.class + " " + section + rollnum; } } let stud1 = { name: "Dinesh", class: "11th", } let stud2 = { name: "Vaibhav", class: "11th", }
let x = student.details.apply(stud2, ["A", "24"]); console.log(x);
`
Output:
Vaibhav 11th A 24
Supported Browser:
- Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 5.5 and above
- Opera 4 and above
- Safari 1 and above
Similar Reads
- JavaScript Handler apply() Method JavaScript handler.apply() method in JavaScript is used as a trap for a function call. The value returned by this method is used as the result of a function call through a proxy. Syntax: const p = new Proxy(target, { apply: function(target, thisArg, argumentsList) { } }); Parameters: This method acc 2 min read
- Kotlin | apply vs with Kotlin :: apply In Kotlin, apply is an extension function on a particular type and sets its scope to object on which apply is invoked. Apply runs on the object reference into the expression and also returns the object reference on completion. It does not simply setting properties of course but do mu 2 min read
- JavaScript Reflect apply() Method Javascript Reflect.apply() method is a standard build-in object in JavaScript which is used to call a function using the specified argument. It works similar to the Function.prototype.apply() method to call a function, but in an efficient manner and easy to understand. Syntax: Reflect.apply(target, 2 min read
- What is the difference between call and apply in JavaScript ? JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr 2 min read
- Function Notation Formula A function is a type of operator that takes an input variable and provides a result. When one quantity is dependent on another, a function is created. An interesting property of functions is that each input corresponds to a single output. In other words, such an operator between two sets, say set A 4 min read
- Currying Functions in Scala with Examples Currying in Scala is simply a technique or a process of transforming a function. This function takes multiple arguments into a function that takes single argument. It is applied widely in multiple functional languages. Syntax def function name(argument1, argument2) = operation Let's understand with 3 min read
- map() Function in R In R Programming Language the Map function is a very useful function used for element-wise operations across vectors or lists. This article will help show how to use it with multiple code examples. Map Function in RThe Map function in R belongs to the family of apply functions, designed to make oper 3 min read
- Scala | Function Composition Function composition is a way in which a function is mixed with other functions. During the composition the one function holds the reference to another function in order to fulfill it's mission. There are some different ways in which a function composition can take place, as below :- compose : Compo 3 min read
- JavaScript Assignment Operators Assignment operators are used to assign values to variables in JavaScript.JavaScript// Lets take some variables x = 10 y = 20 x = y // Here, x is equal to 20 console.log(x); console.log(y); Output20 20 More Assignment OperatorsThere are so many assignment operators as shown in the table with the des 6 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