I was wondering why the Vector variable defined within this self executing javascript function doesn’t require a var before it? Is this just some other type of syntax for creating a named function? Does this make it so we can’t pass Vector as an argument to other functions?
(function() {
Vector = function(x, y) {
this.x = x;
this.y = y;
return this;
};
//...snip
})()
Defining
Vectorany other way would only create it within the scope of the closure; and would not be available outside the closure.Nothing “requires” the
varkeyword; using it defines the scope the variable is available within. Not using it means the variable is created in the global scope.