Put simply, consider the following code snippet:
<script>
window['object'] = function(arg) {
var myvar = 1;
this.object = {};
//add new properties and perform additional tasks
this.object.x = arg?arg+1:myvar;//an example in context
}
//call function once and replace it with object
object(1);
</script>
- Will the function be garbage collected?
- Does it lead to object reuse that could reduce the cost of creating a new object?
This assumes that the function needs local variables or arguments to function properly, thus justifying the need for an actual function to set the object initially.