使用参数数组更改 JavaScript 函数的参数值不起作用 [英] Changing JavaScript function's parameter value using arguments array not working

查看:33
本文介绍了使用参数数组更改 JavaScript 函数的参数值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 JavaScript 并且对 arguments 属性数组感到非常困惑.

I am learning JavaScript and am pretty confused about the arguments property array.

我有一个函数,它接受一个参数并返回它.当我传递参数并使用 arguments[0] = value 重新分配它时,它正在更新值.

I have a function that takes a single argument and returns it. When I pass the parameter and reassign it using arguments[0] = value, it's updating the value.

function a(b) {
  arguments[0] = 2;
  return b;
}
console.log(a(1)); //returns 2

但是当我不带参数调用同一个函数时,它返回undefined.

But when I call the same function with no parameters it returns undefined.

function a(b) {
  arguments[0] = 2;
  return b;
}
console.log(a()); //returns undefined

但即使我通过 undefined,该值也会更新.

But even if I pass undefined, the value will update as well.

function a(b) {
  arguments[0] = 2;
  return b;
}
console.log(a(undefined)); //returns 2

我认为如果您不向 JavaScript 函数传递参数,它会自动创建它并将值分配给 undefined,更新后应该反映更新后的值,对吗?

I thought that if you do not pass a parameter to a JavaScript function, it automatically creates it and assigns the value to undefined and after updating it should reflect the updated value, right?

还有 a()a(undefined) 是同一个东西吧?

Also a() and a(undefined) are the same thing, right?

推荐答案

分配给 arguments 索引只会改变关联的参数值(我们称之为 n-th参数)如果函数被调用至少 n 个参数.arguments 对象的数字索引属性本质上是 setter(和 getter):

Assigning to arguments indicies will only change the associated argument value (let's call it the n-th argument) if the function was called with at least n arguments. The arguments object's numeric-indexed properties are essentially setters (and getters):

http://es5.github.io/#x10.6

下面的斜体字是我对流程与问题的关系的评论:

Italics in the below are my comments on how the process relates to the question:

(Let) args (be) 传递给 [[Call]] 内部方法的实际参数

(Let) args (be) the actual arguments passed to the [[Call]] internal method

  1. len 为 args 中元素的数量.

  1. Let len be the number of elements in args.

indx = len - 1.

重复 while indx >= 0,(因此,当没有参数传递给函数时,下面的循环将不会运行:)

(分配给正在创建的参数对象,这里称为map:)

  1. name 添加为列表 mappedNames 的元素.
  1. Add name as an element of the list mappedNames.

    1. g 成为使用参数 nameenv 调用 MakeArgGetter 抽象操作的结果.
    1. Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.

    1. p 成为使用参数 nameenv 调用 MakeArgSetter 抽象操作的结果.
    1. Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.

    1. 调用map的[[DefineOwnProperty]]内部方法,传递ToString(indx),属性描述符{[[Set]]:p, [[Get]]: g, [[Configurable]]: true}, and false 作为参数.
    1. Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.

  • 因此,如果函数在调用时不带参数,arguments[0] 上不会有 setter,因此重新分配它不会更改索引 0 处的参数.

    So, if the function is invoked with no arguments, there will not be a setter on arguments[0], so reassigning it won't change the parameter at index 0.

    同样的事情也发生在其他索引上——如果你用 1 个参数调用一个函数,但该函数接受两个参数,分配给 arguments[1] 不会改变第二个参数,因为 arguments[1] 没有 setter:

    The same sort of thing occurs for other indicies as well - if you invoke a function with 1 parameter, but the function accepts two parameters, assigning to arguments[1] will not change the second parameter, because arguments[1] does not have a setter:

    function fn(a, b) {
      arguments[1] = 'bar';
      console.log(b);
    }
    fn('foo');

    所以

    a()a(undefined) 是同一个东西吧?

    a() and a(undefined) are the same thing right?

    不是这种情况,因为第二个结果是一个 arguments 对象,在索引 0 上有一个 setter 和一个 getter,而第一个没有.

    is not the case, because the second results in an arguments object with a setter and a getter on index 0, while the first doesn't.

    请注意,arguments 和函数参数之间的这种奇怪的交互仅在草率模式下出现.在严格模式下,对 arguments 的更改不会对单个参数标识符包含的值产生任何影响:

    Note that this odd interaction between the arguments and the function parameters is only present in sloppy mode. In strict mode, changes to arguments won't have any effect on the value an individual argument identifier contains:

    'use strict';
    function a(b) {
      arguments[0] = 2;
      return b;
    }
    console.log(a(1)); //returns 1

    这篇关于使用参数数组更改 JavaScript 函数的参数值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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