Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 04:25 PM
MartinRinehart@gmail.com
Guest
 
Posts: n/a
Default Modify String.prototype?

The FAQ shows a mod to String.prototype adding trim, trimRight and
trimLeft.

Flanagan states, "There is a strong argument against extending built-
in types with your own methods; if you do so, you are essentially
creating your own version of the JavaScript API." And he goes on to
say that this will confuse other programmers who may need to maintain/
extend your code.

Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn't have enumerable
properties, he says. However, Crockford breaks this rule regularly.

Best practice is?
  #2  
Old August 28th, 2008, 05:55 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

MartinRinehart@gmail.com writes:
Quote:
Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn't have enumerable
properties, he says. However, Crockford breaks this rule regularly.
Examples? It's not just to show what not to do?
Quote:
Best practice is?
Don't.

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #3  
Old August 28th, 2008, 07:05 PM
MartinRinehart@gmail.com
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

Lasse Reichstein Nielsen wrote:
Quote:
Examples? It's not just to show what not to do?
"In Chapter 3 we saw that Object.prototype can be augmented.
Array.prototype can be augmented as well." p. 62

Chapter 3 example: beget() method that uses one object to create
another object (of the same "class").
  #4  
Old August 28th, 2008, 07:05 PM
dhtml
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

MartinRinehart@gmail.com wrote:
Quote:
The FAQ shows a mod to String.prototype adding trim, trimRight and
trimLeft.
>
It should instead provide these as functions for the reasons provided by
Flanagan.
Quote:
Flanagan states, "There is a strong argument against extending built-
in types with your own methods; if you do so, you are essentially
creating your own version of the JavaScript API." And he goes on to
say that this will confuse other programmers who may need to maintain/
extend your code.
>
We need an FAQ maintainer.
Quote:
Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn't have enumerable
No disagreement there.
Quote:
properties, he says. However, Crockford breaks this rule regularly.
>
Yes, he does.
Quote:
Best practice is?
One place to modify built-ins is for adding support for features that
are feature-tested as buggy or missing.
  #5  
Old August 28th, 2008, 07:25 PM
dhtml
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

MartinRinehart@gmail.com wrote:
Quote:
Lasse Reichstein Nielsen wrote:
Quote:
>Examples? It's not just to show what not to do?
>
"In Chapter 3 we saw that Object.prototype can be augmented.
Array.prototype can be augmented as well." p. 62
>
Chapter 3 example: beget() method that uses one object to create
another object (of the same "class").
He has a way of getting around this with:-

for(var p in o) {
if(typeof o[p] !== "function") {

}
}

This will have filter out functions that have been added to
Object.prototype.

Modifying Object.prototype has some drawbacks. It requires other users
of API to use a non-standard technique, it will have different
Object.prototype in different frames. Changing what Object.prototype has
creates a dependency on the change and a relationship, so that all
objects now have that functionality.

A cleaner design approach is to resist the temptation to bugger
Object.prototype and create your own object type (or function) to
accomplish the task.

Garrett
  #6  
Old August 28th, 2008, 11:55 PM
Joost Diepenmaat
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

MartinRinehart@gmail.com writes:
Quote:
The FAQ shows a mod to String.prototype adding trim, trimRight and
trimLeft.
>
Flanagan states, "There is a strong argument against extending built-
in types with your own methods; if you do so, you are essentially
creating your own version of the JavaScript API."
This is IMHO essentially correct. The real question is how strong this
argument really is.
Quote:
And he goes on to say that this will confuse other programmers who
may need to maintain/ extend your code.
I'd say that it *might* confuse other programmers. It also might
provide a lot of useful features for programmers not so easily
confused.
Quote:
Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn't have enumerable
properties, he says.
I disagree. IMO, the main reason not to extend the built-in types and
especially Object is that it "breaks"

my o = { ... }
for (var p in o) {
// LOOP
}

in situations where programmers expect LOOP to only iterate over the
immediate properies (excluding the inherited properies) of o. In other
words; when you're using other people's code that may not expect this
behaviour.

This is exactly why Object.prototype.hasOwnProperty() exists, and
personally I think extending Object.prototype is just much too useful
to ignore.
Quote:
However, Crockford breaks this rule regularly.
Crockford is not Flanagan.
Quote:
Best practice is?
I don't know what best practices are. Do whatever you want, but be
careful.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
  #7  
Old August 29th, 2008, 02:25 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

Joost Diepenmaat wrote:
Quote:
MartinRinehart@gmail.com writes:
>
Quote:
>The FAQ shows a mod to String.prototype adding trim,
>trimRight and trimLeft.
>>
>Flanagan states, "There is a strong argument against extending
>built- in types with your own methods; if you do so, you are
>essentially creating your own version of the JavaScript API."
>
This is IMHO essentially correct. The real question is how strong
this argument really is.
Flanagan has a confused notion of what "the javascript API" is to start
with; his book(s) still talk of "client-side" javascript long after that
concept has been abandoned. It would also be trivial to assert that the
creation of any global function was creating your own version of "the
javascript API", or that the purpose of exposing the built-in object
prototypes was so that they could be modified, and so modifying them
cannot be inherently wrong and should not be unexpected.
Quote:
Quote:
>And he goes on to say that this will confuse other programmers
>who may need to maintain/ extend your code.
>
I'd say that it *might* confuse other programmers.
With the likelihood of its confusing other 'programmers' having a
relationship to how well they understand the nature of javascript. Where
a poor grasp of the subject is likely to result in very much else also
being confusing.
Quote:
It also might provide a lot of useful features for programmers
not so easily confused.
>
Quote:
>Flanagan also states, "You must <i>never</iadd properties
>to Object.prototype." Anything added to Object.prototype adds
>enumerable properties to every object, including {}. {}
>shouldn't have enumerable properties, he says.
>
I disagree. IMO, the main reason not to extend the built-in
types and especially Object is that it "breaks"
>
my o = { ... }
for (var p in o) {
// LOOP
}
>
in situations where programmers expect LOOP to only iterate
over the immediate properies (excluding the inherited properies)
of o. In other words; when you're using other people's code
that may not expect this behaviour.
>
This is exactly why Object.prototype.hasOwnProperty() exists,
and personally I think extending Object.prototype is just much
too useful to ignore.
>
Quote:
>However, Crockford breaks this rule regularly.
>
Crockford is not Flanagan.
>
Quote:
>Best practice is?
>
I don't know what best practices are. Do whatever you want, but
be careful.
It is very hard to see extending the prototypes for String, Number,
Boolean or Function as particularly problematic or undesirable. None of
those are likely to be subject to for-in iteration.

The real best practice is probably to take responsibility for what you
are doing an asses the trade-offs and their impacts. The issues mostly
come where there isn't the control to allow accurate judgements to be
made about the impact (i.e. in general purpose library code) where
caution is then advised, otherwise documentation stating the situation
within any given context (either that prototype extensions have been
used and for for-in needs to be handled cautiously, or that for-in has
been used incautiously on particular types so prototypes must not be
extended).

Richard.

  #8  
Old August 29th, 2008, 03:35 AM
Jorge
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

On Aug 29, 3:16*am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
Quote:
>
It is very hard to see extending the prototypes for String, Number,
Boolean or Function as particularly problematic or undesirable.
>
But try to avoid method names such as .clone() or .create()
or .defineProperty().

Use instead names as .myVeryOwnCloner() that are likely not to be ever
chosen to extend the language, when/if it gets ~"officially" extended.

--Jorge.
  #9  
Old August 29th, 2008, 08:45 AM
Gregor Kofler
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

MartinRinehart@gmail.com meinte:
Quote:
The FAQ shows a mod to String.prototype adding trim, trimRight and
trimLeft.
>
Flanagan states, "There is a strong argument against extending built-
in types with your own methods;
I always thought, that's why you have the prototype chain, and the
opportunity to modify the prototype.
Quote:
if you do so, you are essentially
creating your own version of the JavaScript API."
Doesn't compute.
Quote:
And he goes on to
say that this will confuse other programmers who may need to maintain/
extend your code.
There are many more things that can confuse other programmers. Poor
documentation, convoluted scripts, odd method or property names ("$",
"$$", "_$",...), lambdas, etc. Augmented prototypes: not really.
Quote:
Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn't have enumerable
properties, he says. However, Crockford breaks this rule regularly.
Since you have hasOwnProperty() and advisably use it whenever you loop
with for ... in, I don't see the problem. But then I'm probably just a
staunch Crockford fan.
Quote:
Best practice is?
Dunno. I use augmented prototypes and I happy with that. You could
always use a very special prefix for your extensions, to make it as
unique as possible (though I don't).

Gregor


--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
  #10  
Old September 1st, 2008, 12:05 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

Jorge wrote:
Quote:
"Richard Cornford" wrote:
Quote:
>It is very hard to see extending the prototypes for String, Number,
>Boolean or Function as particularly problematic or undesirable.
>
But try to avoid method names such as .clone() or .create()
or .defineProperty().
>
Use instead names as .myVeryOwnCloner() that are likely not to be ever
chosen to extend the language, when/if it gets ~"officially" extended.
Your rationale, if it can be even called so, is unsound at best. Keywords
MUST be and future keywords SHOULD be avoided for identifiers; anything else
is just FUD.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
  #11  
Old September 1st, 2008, 12:15 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

Gregor Kofler wrote:
Quote:
MartinRinehart@gmail.com meinte:
Quote:
>Flanagan also states, "You must <i>never</iadd properties to
>Object.prototype." Anything added to Object.prototype adds enumerable
>properties to every object, including {}. {} shouldn't have enumerable
>properties, he says. However, Crockford breaks this rule regularly.
>
Since you have hasOwnProperty() and advisably use it whenever you loop
with for ... in, I don't see the problem. But then I'm probably just a
staunch Crockford fan.
As for Object.prototype.hasOwnProperty(), it should be noted that it is not
universally available, so augmenting Object.prototype carries with it the
necessity either to provide a method that is equivalent, or to accept that
the library will not be backwards-compatible (to JavaScript < 1.5, JScript <
5.5, ECMAScript < 3). Therefore, I have refactored library code that was
doing it.

However, I, too, see no problem augmenting prototype objects of objects that
are not likely to be subject to for-in iteration (such as String.prototype).
And a good library would provide an iterator method that helped its users
to work around the issue (mine does, in its local version).


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
  #12  
Old September 1st, 2008, 12:35 AM
RobG
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

On Aug 29, 3:38*am, Jeremy J Starcher <r3...@yahoo.comwrote:
Quote:
On Thu, 28 Aug 2008 08:23:09 -0700, MartinRinehart wrote:
[...]
Quote:
Quote:
Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn't have enumerable
properties, he says. However, Crockford breaks this rule regularly.
>
I have never read Flanagan's book, but I have heard some rather negative
reviews about it on this group.
There have been negative comments about some of the things in it,
however it is still a good general reference provided you take its
advice with a grain of salt and do additional research.


--
Rob
  #13  
Old September 1st, 2008, 04:25 AM
Jorge
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

On Sep 1, 12:56*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
Jorge wrote:
>
Quote:
try to avoid method names such as .clone() or .create()
or .defineProperty().
>
Quote:
Use instead names as .myVeryOwnCloner() that are likely not to be ever
chosen to extend the language, when/if it gets ~"officially" extended.
>
Your rationale, if it can be even called so, is unsound at best. *Keywords
MUST be and future keywords SHOULD be avoided for identifiers; anything else
is just FUD.
>
clap clap clap clap, BRAVO !

And how ** do you ** avoid using future keywords in the code written
by yours truly ?

--
Jorge.
  #14  
Old September 1st, 2008, 08:45 AM
optimistx
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

Jorge wrote:
Quote:
On Sep 1, 12:56 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
>Jorge wrote:
>>
Quote:
>>try to avoid method names such as .clone() or .create()
>>or .defineProperty().
>>
Quote:
>>Use instead names as .myVeryOwnCloner() that are likely not to be
>>ever chosen to extend the language, when/if it gets ~"officially"
>>extended.
>>
>Your rationale, if it can be even called so, is unsound at best.
>Keywords MUST be and future keywords SHOULD be avoided for
>identifiers; anything else is just FUD.
>>
>
clap clap clap clap, BRAVO !
>
And how ** do you ** avoid using future keywords in the code written
by yours truly ?
After googling as a stupid newbie , complete idiot and fool, etc, I found
FUD = female urination device.

So I think you can avoid future keywords with it: you simply put your
keys to the device and urinate, I think. If somebody knows better, pls
correct me.


  #15  
Old September 1st, 2008, 07:45 PM
John G Harris
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

On Sun, 31 Aug 2008 at 20:15:43, in comp.lang.javascript, Jorge wrote:
Quote:
>On Sep 1, 12:56*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
>wrote:
Quote:
>Jorge wrote:
>>
Quote:
try to avoid method names such as .clone() or .create()
or .defineProperty().
>>
Quote:
Use instead names as .myVeryOwnCloner() that are likely not to be ever
chosen to extend the language, when/if it gets ~"officially" extended.
>>
>Your rationale, if it can be even called so, is unsound at best. *Keywords
>MUST be and future keywords SHOULD be avoided for identifiers; anything else
>is just FUD.
>>
>
>clap clap clap clap, BRAVO !
>
>And how ** do you ** avoid using future keywords in the code written
>by yours truly ?
Here's an extract from the ECMAScript standard :

7.5.1 Reserved Words
Description
Reserved words cannot be used as identifiers.

Syntax
ReservedWord ::
Keyword
FutureReservedWord
NullLiteral
BooleanLiteral

...

7.5.3 Future Reserved Words
The following words are used as keywords in proposed extensions and
are therefore reserved to allow for the possibility of future
adoption of those extensions.

[Followed by a list of words such as class and static]

So the answer to your question is very simple. You look in ECMA 262 or
equivalent to see which words might be keywords in the future.

Also, it's not that they "should" be avoided; they "must" be avoided.

John
--
John Harris
  #16  
Old September 2nd, 2008, 01:25 AM
Jorge
Guest
 
Posts: n/a
Default Re: Modify String.prototype?

On Sep 1, 8:20*pm, John G Harris <j...@nospam.demon.co.ukwrote:
Quote:
On Sun, 31 Aug 2008 at 20:15:43, in comp.lang.javascript, Jorge wrote:
Quote:
On Sep 1, 12:56*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
Jorge wrote:
>
Quote:
Quote:
try to avoid method names such as .clone() or .create()
or .defineProperty().
>
Quote:
Quote:
Use instead names as .myVeryOwnCloner() that are likely not to be ever
chosen to extend the language, when/if it gets ~"officially" extended.
>
Quote:
Quote:
Your rationale, if it can be even called so, is unsound at best. *Keywords
MUST be and future keywords SHOULD be avoided for identifiers; anything else
is just FUD.
>
Quote:
clap clap clap clap, BRAVO !
>
Quote:
And how ** do you ** avoid using future keywords in the code written
by yours truly ?
>
Here's an extract from the ECMAScript standard :
>
* 7.5.1 Reserved Words
* * Description
* * Reserved words cannot be used as identifiers.
>
* Syntax
* ReservedWord ::
* * Keyword
* * FutureReservedWord
* * NullLiteral
* * BooleanLiteral
>
* ...
>
* 7.5.3 Future Reserved Words
* * The following words are used as keywords in proposed extensions and
* * are therefore reserved to allow for the possibility of future
* * adoption of those extensions.
>
[Followed by a list of words such as class and static]
>
So the answer to your question is very simple. You look in ECMA 262 or
equivalent to see which words might be keywords in the future.
>
Also, it's not that they "should" be avoided; they "must" be avoided.
>
Yes, but the (future) 3.1 extensions may not be in that list. What
happens with them ?
Will current code collide with 3.1 extensions, or will it just shadow
them ?
An Object.prototype.clone() method for example ?

--
Jorge.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles