Most useful built-in object prototype extensions?

M

Matt Kruse

Some libraries go a little overboard (IMO) extending javascript's built-in
types (of course, extending Object is bad).
But extending String, Array, Number, Function with useful methods can make
scripting much simpler and clearer.

What are your nominations for the most useful prototype extensions to
built-in objects?

For example, I find myself liking this very simple one:

/**
* Return true if the string matches any one of the strings passed in as
arguments
*/
String.prototype.oneof = function() {
for (var i=0; i<arguments.length; i++) {
if (this==arguments) {
return true;
}
}
return false;
}

So I can easily code like this:

if (name.oneof("Bill","Bob","John")) {
// Do this
}
else if (name.oneof("X","Y","X")) {
// Do that
}

What else do you guys find really useful?
 
M

marss

Matt said:
What are your nominations for the most useful prototype extensions to
built-in objects?

String.prototype.trim = function()
{
return this.replace(/^\s+|\s+$/g , "" );
}
 
D

Dr J R Stockton

Wed said:
What are your nominations for the most useful prototype extensions to
built-in objects?

One which gives an ISO 8601 date string (YYYY-MM-DD) from a Date Object.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Tue, 16 Jan 2007 10:32:18, "Arnaud Diederen (aundro)" <[email protected]
n.ionicsoft.com> posted:

Unless, of course, one believes programming that is easy enough for
anyone to do (the Americans *can* do it; but lack the will); in which
case -
One which gives an ISO 8601 date string (yyyy-Www-d) from a Date Object.

That's something at which MS, in VBS, have failed; and it's something
which, /inter alia/, ought to be added in ECMA 4.

It's a good idea to read the newsgroup and its FAQ. See below for code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top