Recent Blog Posts
Blogs ยป Archive for April 8th, 2004
Enhancing Javascript’s built in types
Javascript is often falsely derided as a simple language, devoid of the object oriented features so favoured by other modern scripting languages. People holding this opinion really need to take another look at the language, for beneath its beginner friendly interior Javascript packs some powerful language features. In addition to its support for functional programming (where functions can be passed around a script in the same way as the data structures they operate on) Javascript supports a form of OOP known as prototype-based inheritance.
The web is already awash with tutorials on object oriented Javascript, so rather than rehashing them I’m just going to demonstrate one of the neater things you can do with prototype inheritance: add new capabilities to Javascript’s built in types. The following is one of my favourite examples:
Array.prototype.indexOf = function(value) {
for (var i = 0; i < this.length; i++) {
if (this[ i] == value) {
return i;
}
}
return -1;
}
The above code adds a brand new method to the Javascript …
Sponsored Links
SitePoint Marketplace
Buy and sell Websites, templates, domain names, hosting, graphics and more.
Want More Traffic?
Get up to five quotes from qualified SEO specialists, with no obligation!
Download sample chapters of any of our popular books.



