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 used both in different situations, but I never understood the difference, I know that the last approach has overhead creation of functions for an ever instance, but still sees that it is used in many situations, can someone do this for me ? I could not find anything about it by doing a search
source share