Quantcast
Viewing latest article 2
Browse Latest Browse All 4

Answer by Rohit Agrawal for create a new array and push into it in one line - Javascript OOP

The way you are creating the array and pushing value to it is correct. But since it is created inside the function you need to access this object outside the function in some way.

Since you are not returning anything, you can call it as a constructor.

var spot = new Spot()function Spot(value) {  this.x = null;  this.y = null;  this.values = [];  this.values.push(value);}var spot = new Spot();

If you do not want to call it as a constructor than you can simply return this object.

function Spot(value) {  this.x = null;  this.y = null;  this.values = [];  this.values.push(value);  return this;}

Viewing latest article 2
Browse Latest Browse All 4

Trending Articles