In this case, I define an object called DropHandler
function DropHandler(){}
DropHandler.prototype={
AllowDrop : AllowDrop,
Drag : Drag,
Drop : Drop
}
And want to create an instance of DropHandler in Admin object.But what is the different between the following code? code (2) seems can not be used in this case, it will get a undefined type error
var BackendAdmin = function(){
this.DropHandler = new DropHandler();//(1);
var DropHandler = new DropHandler();//(2);
}
The difference is that in (1) you assign new
DropHandlerobject toDropHandlerproperty ofBackendAdmininstance, and in (2) you assign it to a localDropHandlervariable.You get error because
varis moved to the top of the method automatically (it is called Hoisting) by the interpreter and your code in fact looks like:So you try to call the function, which is actually overwriten by an empty variable.
PS: cannot find really good explanation of JS variables hoisting, but this is something from google: http://blog.binarymist.net/2011/11/14/scoping-hoisting-in-javascript/