What are the differences in creating objects in javascript between
test = function(a, b){
this.calculate = function(){
return a + b;
}
}
obj = new test(1, 2);
console.log(obj.calculate());
And
test = function(a, b){
return {
calculate: function(){
return a + b;
}
}
}
obj = test(1, 2);
console.log(obj.calculate());
I have used both in different situations but never understood the difference, I know that the latter approach has the over head of creating the functions for ever instance but still see it used in a lot of situations, can anyone clarafy this for me? I was unable to find anything about this by searching
test.prototypewhile in the second case it directly inherits fromObject.