javascript - Pros and cons of this.myVar vs. var myVar -


this question has answer here:

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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -