

arguments的使用
source link: https://www.fly63.com/article/detial/11307
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

扫一扫分享
arguments 是一个对应于传递给函数的参数的类数组对象。
一、arguments的使用
当我们不确定有多少个参数传递的时候,可以用 arguments 来获取。在 JavaScript 中,arguments 实际上它是当前函数的一个内置对象。所有函数都内置了一个 arguments 对象,arguments 对象中存储了传递的所有实参。
arguments展示形式是一个伪数组,因此可以进行遍历。伪数组具有以下特点:
具有 length 属性
按索引方式储存数据
不具有数组的 push , pop 等方法
利用函数求任意个数的最大值
function maxValue() {
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
console.log(maxValue(2, 4, 5, 9));
console.log(maxValue(12, 4, 9));
在实际开发,建议不要再使用arguments了,请使用ES6的解构语法,比下:
function maxValue(...data) {
let max=data[0]
for (let i = 0; i < data.length; i++) {
if (max < data[i]) {
max = data[i];
}
}
return max;
}
console.log(maxValue(2, 4, 5, 9));
console.log(maxValue(12, 4, 9));
二、arguments.callee的使用
callee是arguments对象的属性。在函数体内,它指向当前正在执行的函数。
ECMAScript 5 禁止在严格模式中使用 arguments.callee()。当一个函数必须调用自身的时候,假如它是函数表达式则给它命名,或者使用函数声明,避免使用 arguments.callee()
使用arguments.callee最常见的情景是当我们要创造一个递归函数的时候:
function factorial(num){
if(num<=1){
return 1;
}else {
return num * arguments.callee(num-1);
}
}
console.log(factorial(4)); //24
但是如果代码是在严格模式下开发,使用"use strict";则会出现报错信息:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
在严格模式下不能通过脚本访问arguments.callee,访问这个属性会报错,那么可以使用命名函数表达式来达到相同的结果:
"use strict";
var factorial = (function f(num){
if(num<=1){
return 1;
}else {
return num * f(num-1);
}
})
console.log(factorial(4)); //24
Recommend
-
117
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 80 million projects.
-
75
README.md ArgWrap ArgWrap is an industrial strength argument wrapping and unwrapping extension for the Vim text editor. It can be used for collapsing and expanding everything from function call...
-
63
README.md evil-args Motions and text objects for delimited arguments in Evil, the extensible vi layer for Emacs. Setup Installing from...
-
63
Parameters and arguments aren't the same thing. Here's an easy way to remember the difference.
-
70
Today, Mozilla is filing our brief in Mozilla v. FCC - alongside other companies, trade groups, states, and organizations - to defend net neutrality rules against the FCC’s rollback that ...
-
66
PHP has a handful of core functions that can accept boolean arguments in the form of constants that have a binary value. These can be combined together in a single function argument, essentially…
-
48
1. Node.js TypeScript #1. Modules, process arguments, basics of the File System In this series, we go through the core concepts of Node.js. In general, in this series, we focus on the environment of
-
62
Keeping objects complete and valid all the time is strategy used in different methodics. It’s perhaps most popular in Domain Driven Design (DDD) . ...
-
32
Adding options to the kernel command line is a common task when debugging or experimenting with the kernel. The upcoming Fedora 30 release made a change to use Bootloader Spec (
-
75
README.md vac-hooks Hook WinAPI functions used by Valve Anti-Cheat. Log calls and intercept arguments & return values. DLL written in C.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK