Reputation: 246
I was trying to create a constructor and inside of that create a new array, I am implementing inside some methods.
My question is what would be the appropriated way of implementing a method inside of an constructor that has an array.
This is my sample of the code:
function Solution(){
var queue = new Array();
}
var sol1 = new Solution();
console.log(sol1.queue);
When I accessed to the array it says undefined. So basically how can I use that array outside of the constructor?
Upvotes: 1
Views: 1924
Reputation: 54089
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
Upvotes: 1
Reputation: 1253
function Solution() {
this.queue = [];
}
Couple of notes:
var
is a variable that is not accessible outside of the function.
In a constructor, this
refers to the object itself.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new:
When the code new Foo(...) is executed, the following things happen:
A new object is created, inheriting from Foo.prototype.
The constructor function Foo is called with the specified arguments, and with
this
bound to the newly created object. new Foo is equivalent to new Foo(), i.e. if no argument list is specified, Foo is called without arguments.The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
Upvotes: 0
Reputation: 94339
You are trying to define an instance property, so why would you use var
to declare a local variable?
Try this instead:
function Solution(){
this.queue = [];
}
This sets the queue
property of the instance to a new array. Remember JavaScript does not have classes. You don't declare variables and hope it will magically appear in the instance.
Upvotes: 1
Reputation: 66
function Solution(){
this.queue = new Array();
//not var, this.
}
var sol1 = new Solution();
console.log(sol1.queue);
Upvotes: 1