I've been always creating JS objects like this a = {}
or this a = new MyConstructor()
without thinking much about it.
Then I came up with some code that looks like this:
function Constructor(){
var private = {
a:1,
b:2
};
return private;
}
var a = new Constructor();
a
naturaly now contains a new instance of the private
object. And then I realised that the new
operator is not required because the private
object gets created every time the Constructor
function gets called.
So the actual question is: What happens when calling new Constructor()
?
Why shouldn't just do a = Constructor()
instead?
What happens to any public properties of the Constructor
object if any?
Is returning objects from a constructor a bad idea?