Here's a piece of JavaScript code that allows you to format strings in a style similar to C#.
String.prototype.format = function() {
var params = String.prototype.format.arguments;
var toReturn = this;
for (var i = 0; i < params.length; i++) {
var regex = new RegExp("\{" + i + "\}", "g");
toReturn = toReturn.replace(regex, params[i]);
}
return toReturn;
}
Original Source: http://chapnickman.com/2006/02/10/string-formatting-in-javascript/
I love this because I can program in a style in which I am familiar. However does this reduce portability or can it lead to a collision? If we all add our own little extensions to the built in objects will we end up with a uncooperative mess? This comes to mind because of the way the very popular prototype javascript library extends the build in JavaScript objects. Some people people criticize prototype.js pointing out that is monolithic or bloated and lacks documentation. While it is rather large it is also very powerful and everthing works together. I don't have to include 5 different libraries from 5 different authors and hope that they work and play together nicely. The documentation criticism is certainly legitimate as the author provides very little documenation. However many sites have posted articles and even whole user guides that have come in very handy.
I haven't even discussed Scriptaculous yet. Scriptaculous is a visual JavaScript framework that uses Prototype for a foundation. I have used this library for several sites and it works well and that is the whole point. Both of these frameworks Just Plain Work! I don't have to cobble something together each time I need a visual effect or an AJAX updater I can just use something in this toolkit.
There is something to be said for re-using the same set of tools. You become very proficient with those tools over time and you can use them more effectively as your experience grows. I find this especially true of using the .NET framework also.
You can't get anywhere by reinventing the wheel each time you need to build a cart. Use other people's experience and work as a foundation for your own and become more productive.