How to set the value of an array from a function?
The issue is because I’ve to change the value of the index before of be set with an variable value.
function foo(arr) {
this.arr=arr;
}
var f = new foo(['a', 'b', 'c']);
// I had thinked in use a function like this one
// Note thta I used index to pass an array wich could
// be greater than 1 dimension.
foo.prototype.setv = function(index, v) {
this.arr.index = v;
}
// but it does not works like I was expecting
idx = [1];
foo.setv(idx, "Z");
This:
Should be this:
In your code you are setting a property of the array, named “index” to a value. This does not actually use the
indexargument passed to your setter function. Using the braket notation for setting allows you to use theindexargument as an actual index for the array.But also, your expected usage is strange:
Why is
idxan array? If you want to set a specific index of the internal array to a value, you would expect to pass in just the index. So that would make more sense to be simply: