I think I have this figured out, but am hoping to cleary have this explained -- what precisely is the difference between these two javascript snippets?
;Person1 = (function() {
var FirstName = 'Hello';
var LastName = 'World';
this.sayHello = function() {
alert(FirstName + ' ' + LastName);
};
});
;Person2 = (function() {
var FirstName = 'Hello';
var LastName = 'World';
this.sayHello = function() {
alert(FirstName + ' ' + LastName);
};
})();
One is being executed with the (); at the end, the other is not. They both perform as expected when I do new Person1().sayHello() or new Person2().sayHello(); -- is it what they leave behind? From what I understand, the latter is a closure that is executed AS IT IS READ by the browser, but I haven't yet fully grasped the implications of that. Thanks for your help!
UPDATE: I mistakenly left out the return keyword in the Person2 closure. I did not fix this, however, because the answers were very helpful in describing the implications of doing this :)