Best way to code Javascript
In order to reduce interferences between several scripts, I know it's highly recommended to encapsulate the Javascript code. But how would you do it? Which would you consider a better script between the following?:
Function 1:
PHP Code:
euler2 = {
getSumEven: function(limit) {
this.total = 0,
this.a = 0,
this.b = 1,
this.abSum = 1;
do {
this.a = this.b;
this.b = this.abSum;
this.abSum = this.a + this.b;
if (this.abSum % 2 == 0) this.total += this.abSum;
} while (this.abSum <= limit)
console.log('euler2. Total: ' + this.total);
}
};
euler2.getSumEven(4000000);
Function 2:
PHP Code:
(function(limit) {
var total = 0,
a = 0,
b = 1,
abSum = 1;
do {
a = b;
b = abSum;
abSum = a + b;
if (abSum % 2 == 0) total += abSum;
} while (abSum <= limit)
console.log('euler2. Total: ' + total);
})(4000000);
I could even exchange the way I use variables so that Function 1 could use local variables as such, and Function 2 would use function properties, and it would also work.
Which way to use better? Which way to use variables?