views:

61

answers:

3

I'm working on a project which makes heavy use of extension methods to convert strings from the UI-layer into their appropriate object-types in the code-layer: (pseudo-code)

// C#

/*
Converts a String from the UI-layer, formatted according to a user-defined
UI-culture preference (in this case from da-DK) into a Double
*/

Double d = "1.000,50".fromWebStringToDouble(); // 1000.5


To unify the programming experience across C# and JavaScript, I want to modify the prototypes of the String, Date and Number object in JavaScript, to implement similar functionality.

Question is: Is this a good idea? We all know that modifying Array's prototype seriously cripples the object. But does this horror-scenario apply to the String, Date and Number object?

+1  A: 

I really like the way prototype adds methodes to those objects, so, for me, if a Framework is doing that, it's OK.

Still, it seems more risky to do so in an application :

  • when using a Framework, you are expecting people to know how it works
  • when developping an application, and adding random methods to Objects, you can't expect people to know all about those -- especially if there is no good documentation about it.

So, in your case, I would only go with that idea if two conditions are met :

  • you are doing this in some kind of Framework, that is common to the whole project (and not in a JS file specific to one or two pages)
  • there is documentation about methods that are added, what they do, and how to use them.
Pascal MARTIN
Conditions are met. Thanks Pascal :)
roosteronacid
If conditions are met, that's fine by me ^^ You're welcome :-) Have fun!
Pascal MARTIN
+2  A: 

The Prototype Library gets away with it. The big issues have been with the Array prototype (people mistakenly thinking Prototype breaks their for..in loops [it doesn't; they were already broken ;-) ]) and with Element prototypes, which are a cross-browser problem. Since you're not talking about doing those, you should be fine.

T.J. Crowder
A: 

I've added a bunch of features to String, Date, Number and even Array via prototype . Can't say that I've experienced any issues with do this. Just avoid doing such a thing to Object that will cause hassle.

AnthonyWJones