I'm new to javascript and after looking a little into javascript scoping and garbage collection, I'm still unsure about what is the right way of doing this.
I want to have a function whose job is to create an object with certain properties and return it. My worry is whether the object created in the function would go out of scope and get dumped. So basically which of the following is better, or are they both wrong?
function createObject() {
var obj = new MyObject();
// set some object properties here
return obj;
}
function createObject2(object) {
// set properties of the input object here
}
I would call my functions like so:
myOjbect = createObject();
myOjbect2 = new MyObject();
createObject2(myObject2);
Basically I'd prefer the first way, but would I have to worry about garbage collection and would it be better to pass a reference of my object into the function instead of creating my object in the function itself?