Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Function apply()

Method Reuse

Theapply() method lets you write a method that can be used on different objects.

Theapply() method is used to call a function with a specificthis.

Theapply() method is similar tocall(), but it passesarguments in an array.

Note

apply() is an advanced topic.

Make sure you understandthis andcall() before continuing.

Thethis Keyword

Thecall() Method


Basic apply() Syntax

Theapply() method is used tocall a function with anobject as an argument.

Theapply() method takesthis as the first argument.

The second argument isan array of values passed to the function.

Theapply() method is similar to thecall() method (previous chapter).

Syntax

functionName.apply(this, [arg1, arg2, ...]);

Using apply() to Setthis

'

When you useapply(), you can decide whatthis should refer to.

In the example below, thegreet function returns a greeting +this.name.

When you useapply(), you decide thatthis should be theperson3 object.

Example

const person1 = { name: "John" };
const person2 = { name: "Paul" };
const person3 = { name: "Ringo" };

function greet(greeting) {
  return greeting + " " + this.name;
}

greet.apply(person3, ["Hello"]);
Try it Yourself »

The Difference Between call() and apply()

The only difference betweenapply() andcall() is how arguments are passed.

Thecall() method takes argumentsseparately.

Theapply() method takes arguments as anarray.

Note

Useapply() when your arguments are already stored in an array.

Example

greet.call(person, "Hello");
greet.apply(person, ["Hello"]);

Borrowing a Method from Another Object

You can useapply() toborrow a method from another object:

Example 1

Apply thefullName method ofperson onperson1:

// Create a person Object
const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}

// Create a person1 Object
const person1 = {
  firstName:"John",
  lastName: "Doe"
}

// Create a person2 Object
const person2 = {
  firstName:"Mary",
  lastName: "Doe"
}

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

Try it Yourself »

Note

In these 2 examples, theapply() method behaves exactly the same as thecall() method.

Example 2

Apply thefullName method ofperson onperson2:

// Create a person Object
const person = {
 fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}

// Create a person1 Object
const person1 = {
  firstName:"John",
  lastName: "Doe"
}

// Create a person2 Object
const person2 = {
  firstName:"Mary",
  lastName: "Doe"
}

// This will return "Mary Doe"
person.fullName.call(person2);

Try it Yourself »


The apply() Method with Arguments

Theapply() method accepts arguments in an array.

All arguments must be placed inside an array, in the correct order.

Example

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

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

person.fullName.apply(person1, ["Oslo", "Norway"]);

Try it Yourself »

Compared with thecall() method:

Example

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

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

person.fullName.call(person1, "Oslo", "Norway");

Try it Yourself »



Simulate Array Methods with apply()

A common use case forapply() is working with Math functions on arrays.

TheMath.max() method returns the highest number in a list of number arguments:

Example

Math.max(4, 8, 5, 1);  // Will return 8

Try it Yourself »

JavaScript Arraysdo not have a max() method, but you cansimulate it with theapply method.

Example

const numbers = [4, 8, 5, 1];

Math.max.apply(null, numbers);

Try it Yourself »

In the example above,null is used asthis, becauseMath.max() doesnot need athis value.

These examples will give the same result:

Examples

Math.max.apply(" ", numbers);

Try it Yourself »

Math.max.apply(0, numbers);

Try it Yourself »


JavaScript Strict Mode

In JavaScript strict mode, if the first argument of theapply() method is not an object,it becomes the owner (object) of the invoked function.In "non-strict" mode, it becomes the global object.


apply() Does Not Create a New Function

Thecall() method and theapply() method runs the function immediately.

They do not return a new function.

Example

function sum(a, b) {
  return a + b;
}

sum.apply(null, [4, 5]);

If you want a function that can be called later with the samethis, usebind() instead.


Common Mistakes

  • Forgetting to Use an Array

    The second argument toapply() must be an array.
  • Confusing apply() with bind()

    apply() runs immediately.bind() returns a new function.
  • Usingapply() whencall() is Simpler

    If you do not have an array,call() is often clearer.

Next Chapter

JavaScript Function bind()



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp