Cases where 'this' is the global Object in Javascript

前端 未结 4 1224
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 11:37

I\'m playing around with different ways to call a function that is an attribute of an Object in Javascript and looking at which type of calls set \'this\' to be the Object a

4条回答
  •  一个人的身影
    2020-11-30 12:07

    In the first 3 examples you use the foo object and run its function, thus you get the context from which the function is run, an Object, the foo object.

    In the last 2 examples you copy the foo.bar function to the variable f and execute it. In this case the function is executed from the global context, not from the foo Object context. It would be the same as declaring and running

      function f() { 
        console.log('this:' + this);
      }
    

提交回复
热议问题