javascript - Pros and cons of this.myVar vs. var myVar -
this question has answer here:
- what scope of variables in javascript? 21 answers
var myfunction = function () { this.myvar = 'something'; var myvar = 'else'; };
what pros , cons of 2 above, see them being used.
the main difference following:
var myfunc = function(){ this.x = 2; // want used outside of declaration } var y = new myfunc(); console.log(y.x); // 2 var myfunc2 = function(){ var x = 3; // want use inside function } var z = new myfunc2(); console.log(y.x); // undefined
so this. variable accessible , var won't be.
Comments
Post a Comment