The are many ways to do the same thing. That is why Javascript is called an expressive language.
Creating the same object via different methods..
Create via new
function Solution(){
this.data = [];
}
var s = new Solution();
console.log(s.data);
or via factory function
function createSolution(){
return { // return object literal
data : [],
};
}
var s = createSolution();
console.log(s.data);
or slight variation on factory function
function createSolution(){
obj = {}; // return object literal
obj.data = []; // add property
return obj;
}
var s = createSolution();
console.log(s.data);
or via direct object literal
var s = { data:[]};
console.log(s.data);
Or use class, or define as prototype, the list goes on.
You are best to use the simplest method possible to keep code complexity down so direct object literal is the best way to create objects (with one exception if you are creating many (1000s to millions plus) instances of the same object define the prototype)
When creating many instance use the following form.
function Solution(){
this.data = [];
};
Solution.prototype = {
data : null, // don't create the array here
}
console.log(new Solution().data); // array output
Defining the property in the prototype saves the javascript interpreter/compiler from having to workout what to add to the new object and parse/compile/optimise , saving cpu time when you do it many times
this.queue = [];