0

I want to make some utility functions for which the obvious place is as a method on some core objects

e.g.

Math.sum = function(arr) { 
  var total=0; 
  for ( x in arr) 
  { 
    total+= parseFloat(arr[x]); 
  } 
  return total;
}

I know that extending core types such as Array is generally considered a bad idea (although I'm not clear on the reasons for that). Would extending the Math object be an equally bad idea?

Obviously I can collect these functions elsewhere, it just seems logical to me that they should reside on the core objects.

DJL
  • 2,060
  • 3
  • 20
  • 39
  • 1
    You can find tons of links on the web about how bad an idea it is to extend these objects. I personnally find no problem in doing so. Although here, you might want to use another name than `sum` – GôTô May 14 '14 at 14:57
  • Just to be clear - Math is an object not a type – DJL May 14 '14 at 14:58
  • Have a look at http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/ – Bergi May 14 '14 at 15:02

1 Answers1

0

First of all you can rewrite the core sum method if it exist. Other point could be the fact that other people will not now about your implementation.

Better way to wrap this object and to add your own implementation.