zoukankan      html  css  js  c++  java
  • 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与形参的关系是通过实参联系起来的

  • 相关阅读:
    灾难 BZOJ 2815
    消耗战 BZOJ 2286
    征途 BZOJ 4518
    纸箱堆叠 BZOJ 2253
    Gate Of Babylon BZOJ 1272
    std::string::npos mean
    [转]整理索引碎片,提升SQL Server速度
    笔记本win7制作wifi
    关闭linux下的使用的端口
    linux多线程
  • 原文地址:https://www.cnblogs.com/feng524822/p/3864359.html
Copyright © 2011-2022 走看看