What’s the difference between
var myView = function () {
//something goes here
};
and
var myView = function () {
//something goes here
return {
a: x,
b: y
}();
I think the first snippet creates a “dynamic” class, so that you can say
var anotherView = new myView();
and the second snippet is similar to a singleton “dynamic” object, but I’m not very sure.
Javascript uses prototypal inheritance, so there are no classes per se. Everything is an object; it’s just that some objects have a common parent object whose methods/variables will be found when name resolution looks up the prototype chain.
Your first code snippet creates an object called
myViewwhose type is a function. Your second snippet defines an anonymous method which returns an object (with two properties,aandb) and then immediately calls this method, assigning the result tomyView. So in this second case,myViewis an object with two self-defined properties.It may help you to read Douglas Crockford’s description of prototypal inheritance in Javascript, as it sounds like you’re a little fuzzy on the details.