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

How to use arguments in JavaScript

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use arguments in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. the use of arguments

When we are not sure how many parameters are passed, we can use arguments to get them. In JavaScript, arguments is actually a built-in object of the current function. All functions have a built-in arguments object, and all arguments passed are stored in the arguments object.

The arguments presentation is a pseudo array, so it can be traversed. Pseudo arrays have the following characteristics:

Has the length attribute

Store data indexed

Push, pop and other methods without arrays

Use the scene:

Using the function to find the maximum value of any number

Function maxValue () {var max = arguments [0]; for (var I = 0; I < arguments.length; iTunes +) {if (max < arguments [I]) {max = arguments [I];}} return max;} console.log (maxValue (2,4,5,9); console.log (maxValue (12,4,9))

In actual development, it is recommended that you no longer use arguments. Please use the deconstruction syntax of ES6, as shown below:

Function maxValue (... data) {let max=data [0] for (let I = 0; I < data.length; iTunes +) {if (max < data [I]) {max=data [I];}} return max;} console.log (maxValue (2,4,5,9)); console.log (maxValue (12,4,9)); II. Use of arguments.callee

Callee is a property of the arguments object. Inside the function body, it points to the function that is currently executing.

ECMAScript 5 prohibits the use of arguments.callee () in strict mode. When a function must call itself, name it if it is a function expression, or use a function declaration to avoid using arguments.callee ()

Use the scene:

The most common scenario for using arguments.callee is when we want to create a recursive function:

Function factorial (num) {if (num)

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