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?
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?