For example, from the book JavaScript: The Good Parts, I stumbled upon this passage:

Much of the complexity of class hierarchies is motivated by the constraints of static type checking. JavaScript is completely free of those constraints. In classical languages, class inheritance is the only form of code reuse.

So what's the relationship between static typing and class hierarchies?


1 Answer 1


In JavaScript, variables (and objects) can change type.

myVar = 'A mere string';
myVar.size = 'big'; // now it is an object with its own properties
myVar.doIt = function(){ /*whatever*/ }; //it even has methods
myVar.prototype.style = 'funky'; // and now its own distinct prototype;
myVar = 9; //nope, it is just a number after all

This is the opposite of static type checking.

An object has many ways of inheriting from another object without being constrained to belonging to the same class. Actually, the whole concept of class is foreign to JavaScript.

This makes for great flexibility. You can have one thousand objects sharing the same properties and behavior, and a thousand-and-first with an extra something, without the cumbersome need of altering the top class and impacting the first thousand.

Of course, this can be confusing at times. If, like most of us, you come from a background of strongly typed OO programing like Java, you'll find yourself in the position of unlearning some of what you know.

See also Object.assign() for a hint of classless inheritance.

One final tip : JavaScript : the Good Parts is an excellent book by an excellent author. It is also very compact, bordering terse or even seemingly cryptic. Don't expect to understand everything at once. It's the kind of books you keep at hand and keep coming back to as your experience grows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.