473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Modifying String.prototype: pro or con?

4 2202
je**********@gmail.com wrote:

http://htmatters.net/htm/1/2006/01/E...n-implicit.cfm

While the recommendation is sound, ISTM that the rationale is not
well-founded. If a future language version or another implementation
would implement a method, augmenting the constructor or prototype
object would most certainly overwrite that method (because we assume a
conforming ECMAScript implementation and we are not talking about host
objects), so there would be no problem with the order of arguments, for
example.

A problem would only arise if the prototype was augmented conditionally,
and the article rightfully questions that approach. Another drawback of
augmentation is that if a future language version or another implementation
would implement that method as augmented, augmenting unconditionally would
probably result in a less efficient implementation of the respective
feature.
PointedEars
Jan 6 '06 #2
je**********@gmail.com escreveu:
http://htmatters.net/htm/1/2006/01/E...n-implicit.cfm


I think it's bad, but I use it sometimes... As the guy said, in the
future the language may add something that you've defined, and your
code will end up breaking the standard...

The only advantage I see is to "prettify" the code and also fix things
that are missing due to different versions, i mean: if(!o.x) o.x = ...

If you modify the prototype of Object or Array, it will damage the
"for..in" for example, so, by modifying the prototype you can face such
situation in the next js version :)
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Jan 7 '06 #3

je**********@gmail.com wrote:
http://htmatters.net/htm/1/2006/01/E...n-implicit.cfm
My vote: Extend (add methods), don't modify (override/overload
methods). Unless you have a strong reason to...
Just because you can, doesn't mean you should though. For instance, a method you
add to String's prototype might require two arguments, but a future update to the
language itself might add the very same method with the arguments reversed.


But your own method, on your page overrides the "updated" version, so
it all still works. And more important, whoever uses the "new improved"
version of JS, must provide an alternative to users using the old
version of JS. You don't. If you feel like it, change your function to
follow the new "standard", but that's not a must.
If you publish "javascript libraries" for the world to use, with your
extensions to function prototypes, you're responsible for all the cases
where it goes wrong. If you use it just in your page, you're
responsible for that page.
I personally was very fond of Array.prototype.contains( ) I made for my
own use and found it useful in several cases...

Jan 7 '06 #4

je**********@gmail.com wrote:
http://htmatters.net/htm/1/2006/01/E...n-implicit.cfm


the ECMA-262 3rd edition standard is dated from 1999

so people comming with the argument that augmenting the prototype
might break a futur version of the language is kind of flawed

first, the next update of ECMA-262 will be ECMAScript 4
and that will occur around 2007

listen to this gillmore podcast with Brendan Eich (timestamp 00h55mn53sec)
http://gillmorgang.podshow.com/?p=25
(you have a slight surely full of errors transcript of the interview here
http://www.zwetan.com/blog/ECMAScrip...crapnotes.html )
second, even with ECMAScript 4 and its syntax ressembling class-based
langauge
you will still be able to augment the prototypes
(from experience with ActionScript 3, being very close to ECMAScript 4)
take an exemple with Firefox 1.5

this browser add some methods to the Array.prototype:
some, filter, forEach, etc...

these methodes are note defined in ECMA-262
1) you can rigthfully override them
2) if you don't override them, you can only use them
in the browser defining them, that means only Firefox 1.5, not the other
browsers
3) you can make your augmentation conditionnal
so Firefox 1.5 users can benefit from these new methods as native
implementation (much faster)
but having those methods accessible to other browsers

this is the problem of the smaller common denominator
do you want every browser (or other hosts) to be able to use those methods
or not ?
now about overriding methods already defined in ECMA-262

let's take for example in Object.prototype : isPrototypeOf,
memberIsEnumerable, hasOwnProperty
some Safari browsers even if claiming they implement the ECMA-262 standard
do not define those methods

then augmenting the prototype of Object object can allow you to "updgrade"
those browsers
you can also do it in a conditional way to not override other browsers
native implementation

this is _also_ the problem of the smaller common denominator
do you want every browser (or other hosts) to be able to use those standard
methods or not ?
The real problem about the pros and cons is about how do you design an
object oriented system

is it more logic for you to define a "trim" method in the String.prototype
or to use a global function "trim" ?

remember also than In OOP global variables are considered bad
http://c2.com/cgi/wiki?GlobalVariablesAreBad
http://c2.com/cgi/wiki?GlobalVariablesConsideredHarmful
etc..

look at how these kind of methods are designed/implemented in other
languages framework as Java, .NET, etc.
the trim method is defined in the String type, not elsewhere

and that's the problem imho with ECMA-262,
it's defining basic core objects, but it's not defining a framework
that's it, ECMAScript is missing what you can call an application framework

the language is very small, very light..but with also a great feature
the language can patch and augment itself using prototype methods
on those same simple core objetcs.

just saying like that it is bad just because tons of people repeating it
like sheep
is a little too much easy imho, the ECMAScript language is made from the
start to be
dynamic, augmenting methods in the prototype can be seen as a very good
thing and a feature
instead of a bad thing to surely not do...

some pros:

one good reason to do that would be good naming
http://c2.com/cgi/wiki?MeaningfulName

another good reason would be to apply polymorphism on the core objects
http://c2.com/cgi/wiki/wiki?PolyMorphism

also in the goal to have a coherent object oriented system the following are
good reasons too
http://c2.com/cgi/wiki?OnceAndOnlyOnce
http://c2.com/cgi/wiki?DontRepeatYourself
look again at the "trim" method and ask yourself some questions:
does it make sens to have this method in any other object than the String
object ?
do you trim Array or Boolean ?
is it logic for an user to apply the trim method to a String object ?
is the "trim" name a meaningfull name for the method ?
why will you do a "for..in" on a String object ?
do you or users using this method will gain time in their everyday
programming task ?
does it work everywhere you plan to use it ?
etc...

but the article is right on one point
"Just because you can, doesn't mean you should though"

you should surely not add anything to the prototype just because you can,
it must be done in a logical, well thought and usefull way.

are you just reacting to the article or do you ask all that because
you're thinking of adding some methods to the prototype and
because you heard a lot that "it is bad" you're not sure ?

zwetan
Jan 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pavils Jurjans | last post by:
Hallo, Is there some decent way how to get the object class name in a string format? Currently I use this: function getClassName(obj) { if (typeof obj != "object" || obj === null) return...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
8
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what...
22
by: Joe Smith | last post by:
It is nothing short of embarrassing to feel the need to ask for help on this. I can't see how I would make the main control for this. What I want is a for loop and a test condition. And while I...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
2
by: rob.nikander | last post by:
Hi, I'm working on a web page that uses the DOM API to move elements within the page in response to an onclick event. My problem is that in Firefox (but not in Safari and IE) I can see...
38
by: Andrea | last post by:
Hi, Anyone could me suggest how to create a function that generates a random string? The function should be: int _RandomString(char** str,int len); so, it takes an empty string str and it puts...
20
by: Xcriber51 | last post by:
Hi -- I'm not entirely familiar with the norms and standard libraries of JavaScript so if the answer to this is yesterday's news, please ignore. I'm trying to write a simple text formatting...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.