这是合法的javascript吗?将参数传递给函数并让它改变它们 [英] Is this legal javascript? Passing arguments to function and having it change them

查看:19
本文介绍了这是合法的javascript吗?将参数传递给函数并让它改变它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这合法吗?它是否适用于所有浏览器?

Is this legal? And does it work in all browsers?

function func1(a, b, c) {
  //b == 2 here
  change_em(arguments);
  //b should equal 3 here
}

function change_em(args) {
  args[0] = 6;
  args[1]++;
  args[2] = [];
}

func1('foo', 2);

如果您想知道,我需要调整函数中的参数.宏将是完美的,除了 javascript 没有.正确地执行此操作,即将变量传递给函数,然后将它们返回到一个对象中,然后在调用者中提取它们几乎与复制/粘贴调整器函数一样多.

If you are wondering, I need to adjust the arguments in a function. A macro would be perfect, except javascript doesn't have one. Doing it properly, i.e. passing the variables to a function, then returning them in an object, and then extracting them in the caller would be almost as much code as just copy/pasting the adjuster function.

推荐答案

在 EcmaScript 3 和 EcmaScript 5 非严格模式下有效,但在 EcmaScript 5 严格模式下无效.它适用于所有现代浏览器和大多数旧浏览器.它不应该在严格模式下工作,例如在带有 "use strict" 指令的 Firefox 5 中运行时.

It is valid under EcmaScript 3 and EcmaScript 5 non-strict, but it is not valid under EcmaScript 5 strict mode. It works in all modern browsers and most old ones. It should not work in strict mode such as when run in Firefox 5 with a "use strict" directive.

来自 EcmaScript 5 规范 10.6.11.c.ii 部分

From the EcmaScript 5 spec section 10.6.11.c.ii

10.6 参数对象

...

如果strict为false并且name不是mappedNames的元素,则

If strict is false and name is not an element of mappedNames, then

  1. 添加名称作为列表mappedNames的元素.

  1. Add name as an element of the list mappedNames.

令 g 为调用 MakeArgGetter 抽象操作的结果名称和环境.

Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.

让 p 是调用 MakeArgSetter 抽象操作的结果名称和环境.

Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.

调用map传递ToString(indx)的[[DefineOwnProperty]]内部方法,属性描述符 {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, false as参数.

Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.

基本上,arguments 对象为每个索引获取一个 setter,因此分配给 arguments[i] 会更改位置 i<的命名参数的值/代码>.规范中有语言,反之亦然.

Basically, the arguments object gets a setter for each index so that assigning to arguments[i] changes the value of the named parameter at position i. There is language in the spec which makes it work vice-versa.

这应该在任何解释器的非严格模式下工作

This should work in non-strict mode in any interpreter

(function (x) {
  alert("x=" + x + ", arguments[0]=" + arguments[0]);  // Both should be 0
  arguments[0] = 1;
  alert("x=" + x + ", arguments[0]=" + arguments[0]);  // both should be 1
  x = 2;
  alert("x=" + x + ", arguments[0]=" + arguments[0]);  // both should be 2
 })(0);

但是如果你在 Firefox 5 上使用 use strict 指令运行上面的代码,你会得到不同的行为:

But if you run the above on Firefox 5 with a use strict directive you get different behavior:

(function (x) {
  "use strict";

  alert("x=" + x + ", arguments[0]=" + arguments[0]);  // Both should be 0
  arguments[0] = 1;
  alert("x=" + x + ", arguments[0]=" + arguments[0]);  // x=0, arguments[0]=1
  x = 2;
  alert("x=" + x + ", arguments[0]=" + arguments[0]);  // x=2, arguments[0]=1 
 })(0);

这篇关于这是合法的javascript吗?将参数传递给函数并让它改变它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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