473,466 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Most useful built-in object prototype extensions?

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[i]) {
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?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 10 '07 #1
4 1761

Matt Kruse wrote:
What are your nominations for the most useful prototype extensions to
built-in objects?
String.prototype.trim = function()
{
return this.replace(/^\s+|\s+$/g , "" );
}

Jan 11 '07 #2
In comp.lang.javascript message <eo*********@news4.newsguy.com>, Wed, 10
Jan 2007 11:03:35, Matt Kruse <ne********@mattkruse.composted:
>
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jan 11 '07 #3
Dr J R Stockton <re*******@merlyn.demon.co.ukwrites:
In comp.lang.javascript message <eo*********@news4.newsguy.com>, Wed, 10
Jan 2007 11:03:35, Matt Kruse <ne********@mattkruse.composted:
>>
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.
Agreed.
[...]
--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:ad@ionicsoft.com
http://www.ionicsoft.com
Jan 16 '07 #4
In comp.lang.javascript message <87************@paddy.ionicsoft.com>,
Tue, 16 Jan 2007 10:32:18, "Arnaud Diederen (aundro)" <ad@remove.the.a.i
n.ionicsoft.composted:
>Dr J R Stockton <re*******@merlyn.demon.co.ukwrites:
>In comp.lang.javascript message <eo*********@news4.newsguy.com>, Wed, 10
Jan 2007 11:03:35, Matt Kruse <ne********@mattkruse.composted:
>>>
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.

Agreed.
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 17 '07 #5

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

Similar topics

4
by: Perttu Pulkkinen | last post by:
Hi all php freaks! Do you think this kind of "property class" is useful or not? I have bee bored to the way I've been coding earlier. because: - often i have database-oriented classes like...
1
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
0
by: kristopher | last post by:
Websites of various subjects ranging from computing to entertainment. TrafficRanking.com says that allsitecafe.com is one of the most VISITED internet sites on the web today! There are over 1,700+...
6
by: erebus- | last post by:
When learning the C programing languages, i have had and still am having the problem of not being able to find answers to many questions. Is their an overall guide/reference that someone knows?
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
3
by: manny | last post by:
Problem: how to have query show only most recent records. This query shows all exams in 2005 for particular individual (grades not shown to avoid embarrassing John Slacker!): SELECT...
0
by: Very cool design! Useful information. Go on!,Very | last post by:
Very cool design! Useful information. Go on!,Very cool design! Useful information. Go on!,Very cool design! Useful information. Go on!
3
by: AnthonyT | last post by:
Hi All I have a major problem with an access query and I am near the end of my tether! I have taken over a project with a badly built access database and as resources are not available to start...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.