最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - create a new array and push into it in one line - Javascript OOP - Stack Overflow

programmeradmin0浏览0评论

I've this code with a function, wherein a I'm trying to define an array then on the next line pushing to it:

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

I've tried this:

this.values = [].push(value);

and

this.values = (this.values || []).push(value);

But failed. Is there something wrong with the code.....

I've this code with a function, wherein a I'm trying to define an array then on the next line pushing to it:

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

I've tried this:

this.values = [].push(value);

and

this.values = (this.values || []).push(value);

But failed. Is there something wrong with the code.....

Share Improve this question edited Nov 21, 2017 at 10:31 popeye asked Nov 21, 2017 at 10:17 popeyepopeye 5011 gold badge4 silver badges17 bronze badges 7
  • What has failed? What error do you get? – hsz Commented Nov 21, 2017 at 10:19
  • 3 this.values = [ value ] – fdomn-m Commented Nov 21, 2017 at 10:21
  • I don't get array made on console screen – popeye Commented Nov 21, 2017 at 10:21
  • damn, how couldn't I just open up my brain a little -- thanks @freedomn-m – popeye Commented Nov 21, 2017 at 10:22
  • Since you are not returning anything, you need to call it as a constructor var spot = new Spot(); – Rohit Agrawal Commented Nov 21, 2017 at 10:22
 |  Show 2 more comments

3 Answers 3

Reset to default 12

You are missing the array-initialisation syntax:

var x = [ 1, 2, 3 ];

in your case, this would be:

this.values = [ value ];

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


The code:

var x = ([]).push("y");

looks like it should generate an array and push the value to it. It does create the array, however the array is not returned to x, the new length of the array is returned, ie 1.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

The push() method adds one or more elements to the end of an array and returns the new length of the array. [emphasis mine]


As noted in the comments, an alternative that is closer to your original attempt would be:

var x = ([]).concat("y");

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Just take the value in an array.

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

var point = new Spot('foo')

console.log(point);

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;
}
发布评论

评论列表(0)

  1. 暂无评论