“不寻常"的Arguments对象的功能 [英] The "unusual" feature of the Arguments object

查看:74
本文介绍了“不寻常"的Arguments对象的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是《 JavaScript,权威指南》中有关arguments对象的引述,来自Core JavaScript Reference:

This is a quote from the Core JavaScript Reference in "JavaScript, the Definitive Guide" regarding the arguments object:

在非严格模式下,Arguments对象具有一项非常不寻常的功能. 当函数具有命名参数时, Arguments对象是包含以下内容的局部变量的同义词: 函数参数. Arguments对象和参数名称 提供两种不同的引用同一变量的方式.改变中 具有参数名称的参数的值会更改以下值: 通过Arguments对象检索,并更改 通过Arguments对象的参数会更改以下值: 通过参数名称检索.

In non-strict mode, the Arguments object has one very unusual feature. When a function has named arguments, the array elements of the Arguments object are synonyms for the local variables that hold the function arguments. The Arguments object and the argument names provide two different ways of referring to the same variable. Changing the value of an argument with an argument name changes the value that is retrieved through the Arguments object, and changing the value of an argument through the Arguments object changes the value that is retrieved by the argument name.

简单来说,这是什么意思?一个例子将是很好的.

In simple terms, what does this mean? An example would be great.

推荐答案

尝试一下:

function x(a, b) {
  arguments[1] = "foo";
  console.log(b);
}

x("hello", "world");

您将在控制台中看到"foo". arguments对象具有类似于数组的属性,这些属性别名由该函数声明的形式参数.这意味着,当您更改arguments[0]时,还将更改第一个显式声明的形式参数的值. JavaScript中没有其他方法可以对变量使用别名,从而使arguments异常".

You'll see "foo" in the console. The arguments object has array-like properties that alias the formal parameters declared by the function. That means that when you change arguments[0], that also changes the value of the first explicitly-declared formal parameter. There's no other way in JavaScript to alias variables, so that makes arguments "unusual".

这篇关于“不寻常"的Arguments对象的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆