JavaScript arguments你不知道的秘密

(function test(x){  
  x=10;  
  console.log(arguments[0], x);  //undefined, 10
})();

(function test(x){  
  x=10;  
  console.log(arguments[0]); // 10
})(1);

(function test(x){  
  x=10;  arguments[0]=2;
  console.log(x, arguments[0]);  //10 ,2
})();

(function test(x){  
  x=10;  arguments[0]=2;
  console.log(x, arguments[0]);  //2, 2
})(1);

(function test(x){  
  x=10;  arguments[0]=2;
  console.log(x, arguments[0]);  //2, 2
})(undefined);
 

由上面函数可得出结论,形参和arguments[i] 是引用关系,但是是在形参调用时有传参的前提下(传入undefined也算传入了参数)

(function test(x){  
  var x = 10;  
  console.log(arguments[0]);  // 10
})(1);

(function test(x,y){  
  var x = 10;  
  console.log(x,arguments[0]);  // 10 ,undefined
})();

上例可以看出 形参如果传值是不会因为在函数内定义同名变量而断开引用的

得出结论:arguments与形参是引用关系;arguments与形参的关系是通过实参联系起来的

原文地址:https://www.cnblogs.com/feng524822/p/3864359.html