What does “this” refer to

后端 未结 2 1390
春和景丽
春和景丽 2021-01-14 04:39

what is the this (inside inner functions) referring to in the following code context? Does it point to TimeSpan?

var TimeSpan = function (da         


        
2条回答
  •  悲哀的现实
    2021-01-14 05:21

    this refers to the current object, in this case the function you are inside of. Since everything in JavaScript is an object you can modify the attributes of a function object using the this keyword:

    var f = function() {
        this.key = "someValue";
    }
    
    console.log(f.key); // prints "someValue"
    

    So in this case this should point to the function at the deepest scope level, and not TimeSpan.

提交回复
热议问题