【问题标题】:Javascript Function Parameter argument or global variableJavascript函数参数参数或全局变量
【发布时间】:2013-08-07 12:49:12
【问题描述】:
var x=10;
function foo(){
    x=120;
    alert(arguments[0]); //outputs 10
}
foo(x);

如何知道我在函数内使用的x是参数还是全局变量?在代码中

如果我提供了这个

function foo(x){
    x=120;
    console.log(arguments[0]);//logs 120
}
foo(x);

这两个代码有什么区别,幕后发生了什么以及参数数组是如何变化的?

【问题讨论】:

  • 在我的 ubunut 12 - chromium 中,它会提示 120 :) 你使用什么浏览器?
  • arguments docs。我还通过控制台在 Chrome 28 中获得了120
  • 我还在 IE9、Firefox 22 和 Chrome 28 中看到 120,通过控制台和 jsfiddle 运行。
  • 我已经编辑了我的问题
  • 我发布了答案,但在此期间,您更改了问题。我希望,无论如何它会帮助你理解......

标签: javascript variables arguments


【解决方案1】:

我如何知道函数内使用的 x i m 是否是参数 还是全局变量?

通过检查函数的参数列表。如果它提到了这样的参数x

function foo(x) {

那么你知道x 是一个参数。它是arguments[0] 的别名(另一个名称),因为它是第一个参数。 arguments 对象不是真正的数组。它具有名称为数字的属性,并且具有长度属性,因此它看起来像一个数组,但它缺少数组的其他特性。

您可以将arguments 视为函数调用中传递的值的最终存储。参数名称是获取存储在arguments 中的值的便捷方式。

如果你的函数开始:

function foo() {

那么它没有参数。您在函数内引用的任何内容都必须是以另一种方式定义的值。它可以是函数内部声明的局部变量:

function foo() {
    var x = 10;

或者它可以引用在你的函数之外声明的名称:

var x;

function foo() {
    x = 10;

在没有“严格模式”的 JavaScript 中,可以这样说:

function foo() {
    x = 10;

从未在任何地方声明var x。结果将是 x 将被创建为全局变量(或全局对象的属性,例如在浏览器中它将创建 window.x)这是一个很大的错误来源,因为您可能拼错变量名称而不会注意到您正在为一个不存在的变量赋值。

【讨论】:

    【解决方案2】:
    var x=10; // this is variable of window namespace
      function foo(x){ 
    // It will make variable of this functions. Every function make its own namespace. 
    // So now you have x in local namespace, so you can't touch x from global 
    // namespace, becouse you gave them same names. With for example function foo(a), 
    // global x would be still accessible. 
    
              x=120; 
    // Here, you change value of x. Program will ask the deepest namespace if there
    // is any x variable. If not, it will continue upper and upper. Right now,
    // we have local x variable, so it will change value of local variable, which
    // will extinct at the end of our function. The most upper namespace is global
    // namespace. One more deeper is window namespace. 
    
              alert(arguments[0]);
    // arguments is Array, which every function has as its property. Via arguments,
    // you can touch every argument, that is passed to function. Later, I will
    // show you an example
            }
      foo(12);
    // finaly here, you call function foo and you set local x = 12. But then,
    // you rewrite local x, so it is 120. After all, you alert first argument,
    // which is local x, which is 120. 
    

    现在进行修改:

    var x=10;
        function foo(a){ // now we have different name for local variable
            x=120; // so our x is x from window namespace
            alert(arguments[0]); // now output is a, which will be 12
            alert(x); // and x is 120
        }
    foo(12);
    alert(x); // x will be still 120... 
    

    有论据的不同故事

    var x=10;
        function foo(){ // we didnt declare any local variable
            x=120; // so our x is x from window namespace again
            alert(arguments[0]); 
            // output is 12, becouse we can touch arguments via 
            // arguments array, even if we didnt declare any local variables
        }
    foo(12);
    

    基本上,参数函数属性允许我们做黑魔法。它使代码难以理解,但它非常强大。很少,使用它们是个好主意。明智地使用它们...

    参数和局部变量指向内存中的同一位置。所以改变一个会立即改变另一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 2020-10-28
      相关资源
      最近更新 更多