var foo1 = function () {
return {
init: function () {
alert(this+" hello foo1");
}
};
}();
var foo2 = {
init: function() {
alert(this+" hello foo2");
}
};
foo1.init()
foo2.init()
The differences that I see are as follows:
- the first is a "closing style", and the second is not.
- the first defined function is a factory (*) function that creates an object and associates the result of this factory with
foo1, and the second is a simple singleton, and you cannot have more instances unless you do. hacking prototype.
Are there any other differences? thisbinding behavior? unexpected browser detonation? crying kittens?
(*) In other words, I could do something like
var fooFactory = function () {
return {
init: function () {
alert(this+" hello foo1");
}
};
}
var foo=fooFactory();
var bar=fooFactory();
and fooand barare now are two different instances of the same "class" (actually, it's just the two objects, which "are" (by construction) have one and the same interface).