6

I have a constructor function in JavaScript as below and want to supply constructor arguments from an array:

 function dataRow(value1, value2, value3, value4, value5) {
                this.val1 = value1;
                this.val2 = value2;
                this.val3= value3;
                this.val4 = value4;               
                this.val5 = value5;
}

Now I want the constructor arguments supplied from an array:

var dataArray = new Array();

dataArray("Value1","Value2","Value3","Value4","Value5","Value6"...........) 

I want (value1, value2, value3, value4, value5) this part to filled from my dataArray.

2
  • Java != JavaScript. They're completely different languages. Commented Jun 8, 2018 at 12:48
  • 1
    Also note that the overwhelming convention in JavaScript is that constructor function names start with a capital letter, to differentiate them from other non-constructor functions. Commented Jun 8, 2018 at 12:51

1 Answer 1

6

In modern environments, you can use spread notation:

var row = new dataRow(...dataArray);

function dataRow(v1, v2, v3, v4, v5) {
  this.v1 = v1;
  this.v2 = v2;
  this.v3 = v3;
  this.v4 = v4;
  this.v5 = v5;
}

var dataArray = [1, 2, 3, 4, 5];
var row = new dataRow(...dataArray);
console.log(row.v1);
console.log(row.v2);
console.log(row.v3);
console.log(row.v4);
console.log(row.v5);

In older environments, it's a real pain to do this. In your specific case, probably just do it the hard way:

dataRow(dataArray[0], dataArray[1], dataArray[2], dataArray[3], dataArray[4]);

Or you can separate creation from the call, and call it with Function#apply:

var row = Object.create(dataRow.prototype);
dataRow.apply(row, dataArray);

function dataRow(v1, v2, v3, v4, v5) {
  this.v1 = v1;
  this.v2 = v2;
  this.v3 = v3;
  this.v4 = v4;
  this.v5 = v5;
}

var dataArray = [1, 2, 3, 4, 5];
var row = Object.create(dataRow.prototype);
dataRow.apply(row, dataArray);
console.log(row.v1);
console.log(row.v2);
console.log(row.v3);
console.log(row.v4);
console.log(row.v5);

That wouldn't work with a constructor created via class, but if you're creating it via class, use spread notation; they were introduced as the same time (ES2015).


I would suggest not doing this, though, especially in light of your comment below:

...how to create this dynamically this.val1 = value1; this.val2 = value2; this.val3= value3;...

Instead, use a single array property, and pass in an array which you either keep or copy:

function dataRow(values) {
  this.values = values;
  // or: this.values = values.slice();
  // to make a shallow copy
}

var dataArray = [1, 2, 3, 4, 5];
var row = new dataRow(dataArray);
console.log(row.values);

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

5 Comments

Thank You for the reply i created below code from your suggestion function dataRow(){ } var row = Object.create(dataRow.prototype); dataRow.apply(row, dataArray); but its not working as well how to create this dynamically this.val1 = value1; this.val2 = value2; this.val3= value3;
"but its not working" Yes, it works; see the live snippets above. "how to create this dynamically this.val1 = value1; this.val2 = value2; this.val3= value3;" Don't. Pass the array into the constructor, and then use an array: this.values = values; or this.values = values.slice(); if you want to (shallow) copy the passed-in array.
I got your point: But in below code snippet i dont want to add the below code manually that is V1,v2,v3, this should be passed from array dynamically depending upon the number of items in a array variable as well i should be able pass this.v1 = v1; this.v2 = v2; this.v3 = v3; this.v4 = v4; from another variable function dataRow(v1, v2, v3, v4, v5) { this.v1 = v1; this.v2 = v2; this.v3 = v3; this.v4 = v4; this.v5 = v5; }
@KumarWarthak - If you pass in the array, you can do string concatenation to create the property names: value.forEach((value, index) => this["v" + (index + 1)] = value; }). But I wouldn't.
If i have a two dataarray like: dataarray =["Value1","Value2","Value3","Value4","Value5","Value6"...........] dataarray1=["this.Value1 = v1;,this.Value2 = v1;,this.Value3 = v1;,this.Value4= v1;, "] i want to use it as below: function dataRow(dataarray) { dataarray1 }

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.