原生JS基础知识(十三)

原生JS基础知识

我的github

this 笔试题

  • 函数预编译过程 this --> window

    function test() {
      console.log(this);
    }
    test();
    
  • 全局作用域里 this --> window

  • call/apply 可以改变函数运行时this指向

  • obj.func(); func()里面的this指向obj

    var person = {
      name: 'xiaoming',
      sayName: function () {
        console.log(this);
        console.log(this.name);
      }
    }
    person.sayName();
    

小知识点

  • Tips : ‘caller’, ‘callee’, and ‘arguments’ properties can not be accessed on strict mode functions and the arguments objects for calls to them

  • arguments.callee 指代当前函数的引用

    function test() {
      console.log(arguments.callee);
      console.log(arguments.callee === test);// true
    }
    
    function test() {
      console.log(arguments.callee);
      function demo() {
        console.log(arguments.callee);
      }
      demo();
    }
    test();
    
    // 应用 : 立即执行函数中使用递归写法
    var num = (function(num) {
      if (num === 1) {
        return 1;
      } else {
        return num * arguments.callee(num - 1);
      }
    }(100));
    console.log(num);
    
  • func.caller 指代当前函数被调用的环境

    function test() {
      demo();
    }
    function demo() {
      console.log(demo.caller);
    }
    test();
    
发布了49 篇原创文章 · 获赞 29 · 访问量 1912

猜你喜欢

转载自blog.csdn.net/Brannua/article/details/104349233
今日推荐