Extending Array functionality safely in Javascript

So I’m using AngularJS to create some tables of data and want to have a simple way to sum the columns at the bottom. I could write a function and attach to $scope however I’d like to be able to use this with a minimum of typing and have it the same in the templates as in Javascript. A good situation to extend Array.prototype.

JavaScript

1

2

3

4

5

6

7

Array.prototype.sum_col=function(col){

vart=0;

for(vari=0;i<this.length;i++)

t+=parseFloat(this[i][col]);

returnt;

};

Nice and easy. However then I noticed that certain pages of my Angular app didn’t load with a very random error. After some debugging in the chrome JS browser it came down to the fact that whilst the above 3-statement for loop works fine, if I now do for( var i in array ) it also includes the sum_col function. D’oh. So after some research on stackoverflow I figured out the correct way to add stuff to Array.prototype which should probably be done by default whenever you want to: