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

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

问题描述

这是合法的吗?它是否在所有浏览器工作?

 函数func1的(A,B,C){
  // B == 2在这里
  change_em(参数);
  // B应该在这里等于3
}功能change_em(参数){
  ARGS [0] = 6;
  ARGS [1] ++;
  ARGS [2] = [];
}func1的('富',2);

如果你想知道,我需要一个功能调整的参数。宏将是完美的,但JavaScript没有之一。在调用者做正确的,即传递变量的函数,然后返回他们在一个对象,然后提取他们将几乎一样多code作为只是复制/粘贴功能的调节。


解决方案

这是ECMAScript的3和ECMAScript 5非严格有效之下,但它不是在ECMAScript的5严格模式有效。它可以在所有现代浏览器,最旧的。它不应该在严格模式下工作时,在Firefox 5使用使用严格的指令运行等。

的ECMAScript 5规范部分10.6.11.c.ii


  

10.6参数对象


  
  

...


  
  

如果严格是假的,并将其命名是不是mappedNames的一个元素,那么


  
  

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


  2.   
  3. 令g调用带参数的MakeArgGetter抽象操作的结果
      名称和ENV。


  4.   
  5. p是调用带参数的MakeArgSetter抽象操作的结果
      名称和ENV。


  6.   
  7. 调用映射传球的ToString(INDX)的[[DefineOwnProperty]]内部方法中,
      属性描述符{[[设置]:对,[[获取]:G,[配置]:真正},而假的
      参数。


  8.   

基本上,参数对象获取二传手每个索引,以便分配给参数[I] 改变在位置上的名称参数值 I 。有在这使得它的工作,反之亦然规范语言

本应在非严格模式下工作在任何跨preTER

 (函数(X){
  警报(×=+ X +,参数[0] =+参数[0]); //两者应为0
  参数[0] = 1;
  警报(×=+ X +,参数[0] =+参数[0]); //都应该是1
  X = 2;
  警报(×=+ X +,参数[0] =+参数[0]); //这两个应该是2
 })(0);

但是,如果你用一用严格的指令运行在Firefox 5上面你会得到不同的行为:

 (函数(X){
  使用严格的;  警报(×=+ X +,参数[0] =+参数[0]); //两者应为0
  参数[0] = 1;
  警报(×=+ X +,参数[0] =+参数[0]); // X = 0,参数[0] = 1
  X = 2;
  警报(×=+ X +,参数[0] =+参数[0]); // X = 2,参数[0] = 1
 })(0);

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);

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.

解决方案

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.

From the EcmaScript 5 spec section 10.6.11.c.ii

10.6 Arguments Object

...

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

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

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

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

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

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);

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天全站免登陆