Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between call and apply in javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about the differences between call and apply in javascript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

In JavaScript, the call () method and the apply () method do the same, except that the parameters are received in different ways; when using the call () method, the parameters passed to the function must be enumerated one by one, and when apply () is used, the parameter array is passed to the function.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the difference between call and apply in javascript

The call () method calls a function with a specified th value and parameters supplied separately (a list of parameters).

Note: the function of this method is similar to that of the apply () method, except that the call () method accepts a list of several parameters, while the apply () method accepts an array of multiple parameters.

Grammar.

Fun.call (thisObj [, arg1 [, arg2 [,...])

Definition: calls a method of one object to replace the current object with another object.

Description:

The call method can be used to call a method instead of another object. The call method changes the object context of a function from the initial context to the new object specified by thisObj.

If no thisObj parameter is provided, the Global object is used as a thisObj.

Parameters.

ThisObj

The value of this specified when the fun function is running. It is important to note that the specified this value is not necessarily the true this value when the function is executed, and if the function is in non-strict mode, it is specified as null and undefined

The this value automatically points to the global object (that is, the window object in the browser), while the this whose value is the original value (number, string, Boolean value) points to the automatic wrapper object for that original value.

Arg1, arg2,...

The specified list of parameters.

Return value

The returned result includes the specified this value and parameters.

Apply ()

The apply () method calls a function with a specified th value and parameters provided as an array (or an array-like object).

Grammar

Fun.apply (thisObj, [argsArray])

Definition: apply a method of an object and replace the current object with another object.

Description:

If argsArray is not a valid array or is not an arguments object, it will result in a TypeError.

If no argArray or thisObj parameters are provided, the Global object will be used as a thisObj and no parameters can be passed.

Parameters.

ThisObj

The this value specified when the fun function is running. It is important to note that the specified this value is not necessarily the real this value when the function is executed, but if the function is in non-strict mode, specify

When null or undefined, it automatically points to the global object (the window object in the browser), and the this whose value is the original value (number, string, Boolean value) points to the automatic wrapper object for that original value.

ArgsArray

An array or class array object in which the array elements are passed to the fun function as separate arguments. If the value of this parameter is null or undefined, no parameter needs to be passed in. From ECMAScript 5

At first, you can use class array objects.

Summary

The two functions are the same, binding obj (that is, this) to thisObj, when thisObj has the properties and methods of obj. Or thisObj "inherits" the properties and methods of obj.

The only difference is that apply accepts array parameters and call accepts continuous parameters.

The call () method and the apply () method do the same thing, but they differ in the way they receive parameters. For call (), the first argument is that the value of th does not change, but the rest of the arguments are passed directly to the function. When using the call () method, the arguments passed to the function must be enumerated one by one. When using apply (), the argument array passed to the function is explained by the following code:

Function add (c, d) {return this.a + this.b + c + d;} var o = {a var 1, bv 3}; add.call (o, 5,7); / 1 + 3 + 5 + 7 = 16add.apply (o, [10,20]); / 1 + 3 + 10 + 20 = 34

Both call and apply exist to change the context or context of a function's runtime, in other words, to change the direction of the this inside the function body. Because the functions of JavaScript have the concepts of "definition-time context" and "runtime context" and "context is changeable".

The two functions are exactly the same, except that the way you accept parameters is not quite the same. For example, there is a function fun that is defined as follows:

Var fun = function (arg1, arg2) {}

It can be called via fun.call (this, arg1, arg2); or fun.apply (this, [arg1, arg2]);. Where this is the context you want to specify, it can be any JavaScript object (everything in JavaScript is an object), call needs to pass the parameters in order, and apply puts the parameters in an array.

In JavaScript, the number of parameters of a function is not fixed, so if applicable, use call when your parameters are clearly known, and when you are uncertain, use apply, and then pass the parameters push into the array. When the number of parameters is uncertain, the arguments array can also be used inside the function to traverse all the parameters.

These are all the contents of this article entitled "what are the differences between call and apply in javascript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report