阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      JavaScript 函数的三种角色

      函数的三种角色

      var num = 0;
      function Fn(num) {
        arguments[0] = 100;
        this.num = 200;
        console.log(num);
        console.log(this.num);
        return num;
      }
      
      Fn.num = 400;
      Fn.prototype.num = 300;
      Fn.prototype.getNum = function () {
          console.log(this.num);
      };
      
      var f = new Fn(num);
      console.log(f.num);
      f.getNum();
      Fn.prototype.getNum();
      console.log(num);
      
      var n = Fn(num);
      console.log(n);
      console.log(num);
      

      解析思路如下:

      var num = 0;
      function Fn(num) {
        arguments[0] = 100;
        this.num = 200;
        console.log(num);//100
        console.log(this.num);//200
        return num;
      }
      
      Fn.num = 400;
      Fn.prototype.num = 300;
      Fn.prototype.getNum = function () {
          console.log(this.num);
      };
      
      var f = new Fn(num);
      console.log(f.num);//200
      f.getNum();//200,
      Fn.prototype.getNum();//300
      console.log(num);//0
      
      var n = Fn(num);
      console.log(n);//100,200,100
      console.log(num);//200
      目录
      目录