var tools = {};
tools.triangle = function() {
var originX = 0;
var originY = 0;
}
var tools = {};
tools.triangle = function() {
this.originX = 0;
this.originY = 0;
}
Are there any differences between these two code blocks? Sorry if this has been asked before.
varcreates a local variable withintools.triangle. The variablesoriginXandoriginYcannot be interacted with outside oftools.triangle.thisis a pointer to the current object you are dealing with. The second example can be used to give properties to an object by doingnew tools.triangle();. If you do not usenewand just usetools.triangle();,thiswill point the global object which is thewindowobject. You can change the object to whichthispoints by using the function methodscall();andapply();like this:It is important to know that
thiscan reference any object, as well as be undefined ornullin ES5 strict mode.You can find more information here.