It’s time to embrace some new features

A modern approach to object creation in JavaScript
Keine Kommentare

After stalling for several years, we’ve recently seen JavaScript language development making a fair amount of progress. The ECMAScript5-standard [1] is now almost 3 years old and all major browser vendors support it in their current versions. So this may be the right time to really embrace new language features and adopt a modern style of development. If you use JS for more than just jQuery-one-liners, you will need a structured approach. There are many ways to structure your code in JS. One great and also very common way to do it is to use objects and prototypical inheritance.

The old way

Let’s assume you build a shop and you need to express products. In the old days – before ECMAScript5 (ES5), you would have written something like this:

var Product = function(name) { = name;


Product.prototype.showName = function() {alert(;};

var myProduct = new Product(„Super Fancy TV“);


Actually, this isn’t bad. You have a constructor to create your objects and attach methods to the prototype. All individual products have these methods, but there are several weak points to consider:

  • There are no private attributes in the JavaScript language. The value of name can be changed any time from the outside.
  • The methods are scattered. Even if you put them all in one place, there is no single structure to define your object concepts – like a class in many other object oriented languages.
  • You can easily forget to use the “new” keyword. This won’t throw an error, it just leads to completely different behaviour and probably to some really nasty bugs that are hard to find.
  • Inheritance will provide even more problems. There isn’t an agreed way in the JS-community of how to do it properly.


Unsere Redaktion empfiehlt:

Relevante Beiträge

- Gib Deinen Standort ein -
- or -