Difference between var and this in Javascript functions?

后端 未结 2 1958
刺人心
刺人心 2020-11-28 12:53
var tools = {};

tools.triangle = function() {
    var originX = 0;
    var originY = 0;
}

 

var tools = {};

tools.triangle =         


        
2条回答
  •  死守一世寂寞
    2020-11-28 13:19

    In the first example, X and Y both exist as local variables to the closure saved in the variable triangle.

    In the second example, X and Y exist as variables to the object tools.triangle because of the use of this.

提交回复
热议问题