JavaScript advanced series—arguments object
Convert to array
Pass parameters
Automatic update
Performance Truth
#Every function in JavaScript can access a special variable arguments. This variable maintains the list of all parameters passed to this function.
arguments variable is not an array (Array). Although syntactically it has the array-related property length, it does not inherit from Array.prototype and is actually an Object.
(Note: Since arguments have been defined as a variable within the function. Therefore, defining arguments through the var keyword or declaring arguments as a formal parameter will result in native arguments not being created.)
Therefore, standard array methods such as push, pop or slice cannot be used on the arguments variable. Although it is possible to use a for loop to traverse, in order to better use the array method, it is best to convert it into a real array.
Convert to array
The following code will create a new array containing all elements in the arguments object.
Array.prototype.slice.call(arguments);
This conversion is relatively slow and is not recommended in code with poor performance.
Passing parameters
The following are recommended practices for passing parameters from one function to another.
function foo() { bar.apply(null, arguments); } function bar(a, b, c) { // 干活 }
Another trick is to use call and apply together, creating a quick unbinding wrapper.
function Foo() {} Foo.prototype.method = function(a, b, c) { console.log(this, a, b, c); }; // 创建一个解绑定的 "method" // 输入参数为: this, arg1, arg2...argN Foo.method = function() { // 结果: Foo.prototype.method.call(this, arg1, arg2... argN) Function.call.apply(Foo.prototype.method, arguments); };
Translator's Note: The above Foo.method function has the same effect as the following code:
Foo.method = function() { var args = Array.prototype.slice.call(arguments); Foo.prototype.method.apply(args[0], args.slice(1)); }
Automatically updates the
arguments object to its internal properties and function form Parameters create getter and setter methods.
Therefore, changing the value of the formal parameter will affect the value of the arguments object, and vice versa.
function foo(a, b, c) { arguments[0] = 2; a; // 2 b = 4; arguments[1]; // 4 var d = c; d = 9; c; // 3 } foo(1, 2, 3)
Performance Truth
The arguments object is always created regardless of whether it is used, except in two special cases - declared as a local variable and as a formal parameter.
arguments getters and setters methods are always created; therefore using arguments has no impact on performance. Unless you need to access the properties of the arguments object multiple times.
Translator's Note: The description of arguments in strict mode in MDC is helpful for our understanding. Please look at the following code:
// 阐述在 ES5 的严格模式下 `arguments` 的特性 function f(a) { "use strict"; a = 42; return [a, arguments[0]]; } var pair = f(17); console.assert(pair[0] === 42); console.assert(pair[1] === 17);
However, there is indeed a situation that will have a significant impact. Performance of modern JavaScript engines. This is using arguments.callee.
function foo() { arguments.callee; // do something with this function object arguments.callee.caller; // and the calling function object } function bigLoop() { for(var i = 0; i < 100000; i++) { foo(); // Would normally be inlined... } }
In the above code, foo is no longer a simple inline function inlining (Translator's Note: This refers to the parser that can do inlining processing), because it needs to know itself and its calls By. Not only does this negate the performance gain of inline functions, but it breaks encapsulation, so now functions may depend on a specific context.
# Therefore, it is strongly recommended that you do not use arguments.callee and its properties.
The above is the content of the JavaScript advanced series—arguments object. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
