Suppose I have a function:
function test(){
//should I define global variable here
//like window.arr=new Array()?
}
//can I define here
//window.arr=new Array()?
test.prototype.method01=function(){
//or here:window.arr=new Array()?
}
Of the three ways above, which one is better?
If it’s a global, you will more than likely want to define it outside of a function. This is because if it’s global, you want it to be accessible by any/all functions.
JavaScript is interpreted as it comes in. If you define it outside the function it will be declared as it is interpreted, if it is called inside the function, it will be declared as the function is called.