1

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?

1
  • this.queue = []; Commented Jul 12, 2016 at 2:13

4 Answers 4

1

function Solution(){
  this.queue = new Array();
  //not var, this.
}

var sol1 = new Solution();
console.log(sol1.queue);

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

In ECMAScript 6, however, JavaScript have classes.
@Chalk Well true, but classes in ES6 is just syntactical sugar over the existing prototype-based syntax: it doesn't have extra functionality.
1

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

Comments

0
function Solution() {
  this.queue = [];
}

Couple of notes:

  1. var is a variable that is not accessible outside of the function.

  2. 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:

  1. A new object is created, inheriting from Foo.prototype.

  2. 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.

  3. 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.)

2 Comments

Thanks for this explanation. That's exactly what I need to know. I completetely forget that if I declare a variable with var the variable is private. If I declare with this the variable is accesible to other functions. Thanks!
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.