arguments

本文详细探讨了JavaScript中Arguments对象的特性和用法,包括其作为类数组对象的基础概念、如何安全地转换为数组、在不同模式下的行为差异、递归调用、参数传递以及实现重载等高级技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是arguments:它是一个类数组对象,代表传给一个function的参数列表。它的内容表示了函数执行时传入函数的所有参数,可以用arguments[0],arguments[1]...来获取单个参数。可以用arguments.length来获取参数个数

         <script>
		function test(){
			console.log(arguments);
		}
		test('a','b',0,{foo:'hello arguments'});
	</script>
2.通常可以采用Array.prototype.slice.call(arguments);来将其转换成数组,或者[].slice.call(arguments);但是不能将函数的arguments泄露或者传递出去,如
// Leaking arguments example1:
function getArgs() {
    return arguments;
}

// Leaking arguments example2:
function getArgs() {
    const args = [].slice.call(arguments);
    return args;
}

// Leaking arguments example3:
function getArgs() {
    const args = arguments;
    return function() {
        return args;
    };
}
但是我们可以这样:
function getArgs() {
    const args = new Array(arguments.length);
    for(let i = 0; i < args.length; ++i) {
        args[i] = arguments[i];
    }
    return args;
}
3.修改它的值

它有隐藏的参数

window.onload = function(){
    abc(1,2,3);
}
function abc(x,y){
    alert(x+','+y);//1,2
	}
在严格模式下:
function test(a){
	"user strict";
	console.log(a,arguments[0]);//1,1
	a = 10;
	console.log(a,arguments[0]);//10,1
}
test(1);
在非严格模式下:
function test(a){
	console.log(a,arguments[0]);//1,1
	a = 10;
	console.log(a,arguments[0]);//10,10
}
test(1);

实现递归:

function add(n){
	if(n==1){
	return 1;
	}else{
	return n+arguments.callee(n-1);
	}
}
alert(add(3));
4.函数之间参数的传递

function test(a){
	b.apply(this,arguments);
}
function b(){
	console.log(arguments);//[1]
}
test(1);
5.利用arguments实现重载

在javascript中没有重载

function add(num1, num2) {
    console.log("Method one");
    return num1 + num2;
}

function add(num1, num2, num3) {
    console.log("Method two");
    return num1 + num2 + num3;
}

add(1, 2);//Method two
add(1, 2, 3);//Method two
but...
function add(num1, num2, num3) {
    if (arguments.length === 2) {
        console.log("Result is " + (num1 + num2));
    }
    else if (arguments.length === 3) {
        console.log("Result is " + (num1 + num2 + num3));
    }
}

add(1, 2);//Result is 3
add(1, 2, 3)//Result is 6
6.es6中的arguments

扩展操作符可以将 arguments 展开成独立的参数。

function test(a){
	console.log(...arguments);
}
test(1,2,3);//1 2 3
7.类数组

我们可以自己创建一个类数组:

function Foo(){
}
Foo.prototype = Object.create(Array.prototype);
const foo = new Foo();
foo.push('a');
console.log(foo,foo.length);
此时的Foo示例拥有Array的所有方法,但是类型不是Array.








### 关于 Arguments 的定义与作用 在编程或脚本编写上下文中,`arguments` 是指传递给函数、过程或命令的一组输入值。这些参数可以用于控制程序的行为或者提供数据处理所需的变量[^1]。 当提到 `arguments` 时,通常会涉及两种主要场景: #### 函数调用中的 Arguments 在函数调用过程中,`arguments` 表示实际传入的值列表。它们被用来初始化函数签名中声明的形式参数(formal parameters)。例如,在 Python 中,如果有一个函数如下所示: ```python def add(a, b): return a + b ``` 那么在这个例子中,`a` 和 `b` 就是形式参数,而当你这样调用该函数时: ```python result = add(3, 5) ``` 这里的 `3` 和 `5` 则被称为实参(actual arguments 或 actual parameters),即具体的数值作为输入传递给了函数。 #### 命令行工具中的 Arguments 对于 shell 脚本或其他类型的命令行应用程序而言,`arguments` 还可能指的是通过命令行界面 (CLI) 提供给可执行文件的数据片段。比如运行某个 Bash 脚本时可能会这样做: ```bash ./script.sh arg1 arg2 arg3 ``` 在此情况下,`arg1`, `arg2`, 和 `arg3` 都属于命令行参数范畴,并可通过特定语法访问,像 `$1`, `$2`, 等等来获取各个位置上的 argument 值。 另外值得注意的是,在某些高级面向对象的语言里如 Smalltalk 中也有自己独特的表达方式去描述 statements 及其组成部分,其中也包含了如何构建以及解析 arguments 的机制[^2]。 ### 示例代码展示 Argument 使用情况 下面给出一段简单的 JavaScript 实现展示了匿名函数内部 this 对象指向问题解决办法之一——利用 bind 方法绑定指定this值到回调函数上从而确保正确引用外部 variables/properties。 ```javascript const person = { name: 'John Doe', greet: function() { console.log(`Hello from ${this.name}`); } }; setTimeout(person.greet.bind(person), 1000); // Output after one second delay will be "Hello from John Doe" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值