I'm coming from AS3 to HTML5 javascript so this is kinda confusing:
1.
I noticed that inside objects, you do not need to declare vars:
var player = {
type:'player',
x:50,
}
The same thing with vars (var type, var x) display errors, why is that?
2 . Why don't I need to declare a var in a function:
createPlayer = function(){
a = 5;
console.log(a); //works
}
3. I assume that inside a function, once you create a new var like this:
var a; //global var
createPlayer = function(){
a = 3;
}
it first searches to see if there's a global var called a, if it already exists it changes it's value, if not it creates it locally in the function. is that correct?
- Generally, when do I need to declare a var, and when not?
Thank you for your time.