Learn Node.js The Troll Way
nathanleclaire.com
nathanleclaire.com
Don't do this. Extending native prototypes in node is terrible practice since it ruins the thing node gets very right: code encapsulation. Not good advice at all.
Is it "always" correct to NOT extend the String prototype? No.
Like all things it depends. If you are writing a library for others to use then you shouldn't be extending globals for the reasons you have given. And if you do, you need a good reason and make it very clear to the user. If you are writing an application or a closed-source library, then it is usually safe to extend globals because you are in complete control. Like others have said in the String case, it makes its a lot easier to read the functions that do string manipulation.
The only absolute rule in software engineering is that there are no absolute rules.
I admit that extending the String prototype is less dangerous than e.g. the Object or Array prototype.
I personally love monkey patching system libraries, it makes your code look pretty. I guess it's one of the guilty pleasures of a Ruby developer.
On the subject of awesome languages like Ruby, did you know that C# actually has a way of safely monkey patching system libraries? It's called 'extension methods' and it's actually the bees knees. Too bad C# has a proprietary side that makes it less desirable for business critical software, leaving us with Ruby.
By not messing with globals, node allows you to safely load multiple versions of the same module and not worry about strange runtime environment changes. It probably doesn't matter in this trivial case, but node's primary win is how easy and safe it is to write and share modular code so yeah, prototype pollution is a destructive anti-pattern.
I think you've got that around the wrong way. It's more of a problem in node/browserify because you're typically working with very many more 3rd party libraries.
if ( $(index).html().match(/^".*"$/) ) {
return true;
} else {
return false;
}
Gah!