You can't override that accessor of Arrays.
Here an example:
Object.defineProperty(Array.prototype, 0, {
get: function () { return "my get on 0"; }
});
var a = [1,2,3];
console.log(a[0]); // output: 1
But if you try to do the same with a property which doesn't really exist in the array, you will achieve it:
Object.defineProperty(Array.prototype, 5, {
get: function () { return "my get on 5"; }
});
var a = [1,2,3];
console.log(a[5]); // output: my get on 5
What you can do is a little workaround accessing the elements through the get
method of Arrays.
Array.prototype.get = function(i) {
console.log('my print');
console.log(this[i]);
return "this is!";
};
var a = [1,2,3];
console.log(a.get(0)); // output: my print 1 this is!
So, coming back to your question you could do something like you did for push
but with the get
, avoiding proxies:
Array.prototype.get = function (i) {
console.log('Accessing element: ' + this[i]);
console.log(this);
return this[i];
};
var array = [1, 2, 3];
var total = 0;
// be careful that now you cannot do anymore
// for (var i in array), because inside the array there is also the property get defined and it will cycle also on that
// if you want to cycle again in that way, you need the check through hasOwnProperty method
/*
for(var i in array) {
if (array.hasOwnProperty(i)){
console.log(i);
total += array.get(i);
}
}
*/
for(var i = 0; i < array.length; i++) {
total += array.get(i);
}
console.log(total);
Just for completing the answer, what you are trying to do can be done in one line with reduce
method of Arrays:
var array = [1, 2, 3];
var result = array.reduce(function (accumulator, actual) {
return accumulator + actual;
}, 0);
console.log(result);
I strongly recommend you to avoid the override of these accessors. You will change the basis of the code so it will be impossible for third party people to understand what's going on without reading all the code. Moreover you will lose a lot of built-in useful methods.
I hope this helps
p.s. following to your edit, for checking undefined values and raise exceptions you can add the check inside the override of the get method.
But my suggestion is just to filter the array, detect the undefined values and get rid of them.
Note that I am using the double equal. because undefined == null
but undefined !== null
.
In this way you will remove both undefined and null values. If you want to remove only undefined, change it to if (typeof element === 'undefined')
.
So something like this, using just one loop with arrays filter
method:
var data = [1, 2, undefined, 3, 4, undefined, 5];
data = data.filter(function( element, index ) {
// note that I am using the double equal. because undefined == null but undefined !== null.
// in this way you will remove both undefined and null values
// if you want to remove only undefined, change it to if (typeof element === 'undefined')
if (element == null) {
console.log('found and undefined null value at index: ' + index);
}
return element != null;
});
console.log(data); // array without undefined and null values