JavaScript Doesn't Need Class
Written by Ian Elliot   
Friday, 18 November 2011
Article Index
JavaScript Doesn't Need Class
JavaScript Doesn't Need Type

Banner

 

JavaScript doesn't use type.

You don't have to worry about a hierarchical type system and hence you can forget inheritance. You can simply create objects and add properties and methods to them as you like. Even in the world of strongly typed object oriented languages the tide has turned against inheritance and in favour of aggregation. In JavaScript aggregation is natural and inheritance is a strange add-on that really belongs in other languages.

If this is the case how can you know what properties and methods an object has?

The simple answer to this is that you are the programmer so don't call a method or property unless you know or check that it exists. 

You can either program in a relaxed style knowing that the objects that you are using conform to the requirements or you can program defensively and put checks in to make sure that methods and properties exist. This is just another example of the test for facilities rather than test for the browser that nearly all web programmers have adopted.

In JavaScript you don't enforce interfaces as the object level but by making sure that the methods and properties are actually present and ready to be used.

Put another way -

in JavaScript you never have to ask an object its type, only what it can do.

 whatcanyoudo

 

Notice also that you don't have to invent the idea of overriding methods, virtual methods or anything similar you can simply change a method when ever you need to. There are no function signatures - because there is no type - and there is no function overloading.  Again when you call a function it is up to the function to make what it will of the parameters you pass it.

Of course we don't need generics either - as there is no type we don't need to invent a way to specify algorithms in a type free way.

 

Finally, what about the assertion that JavaScript uses prototype inheritance?

Well this isn't quite correct.

JavaScript's object construction is fine without prototype, but it has an inefficiency. When you use a class based language each instance you create has its own data storage but all instances share the same code. In JavaScript and other untyped object oriented languages objects instances have their own data and code. Usually this doesn't matter as the overhead of duplicating code is usually not great - unless it is a lot of code or a lot of duplication.

The prototype mechanism is actually a way of allowing different instances to share the same code. It works by having a prototype object which defines the shared methods. When you create an instance, the instance has access to the shared methods as if they were defined locally - it really is that simple.

 

Prototypes are about shared code.

If you have had a lifetime of classical strongly typed static programming, you will probably be fuming that all of the rigorous coding conventions have just been dumped in favor of something that sounds so unstructured that they only thing that could come out of it are errors.

This isn't the case.

Good JavaScript programming has its checks and balances just like class based programming but don't expect them to be the same.

 

JavaScript does have its problems but the lack of a class construct, inheritance and strong typing shouldn't be on the list. Some of its problems have been generated by misguided attempts to graft type and class based inheritance on top of the original language.

There are some inherent problems with JavaScript, some minor and some more major, but turning it into Java or C# isn't the way to improve it. If anything JavaScript should move in the direction of Ruby or any modern dynamic language not back towards Java or C#.

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter or Facebook or sign up for our weekly newsletter.

 

 

Banner


Getting Started With jQuery - Filters

Mastering the core of jQuery is first a matter of understanding selectors and then DOM manipulation. Filters are often confused with selectors but they are quite different and serve an important purpo [ ... ]



Javascript data structures - the binary tree

Binary trees in JavaScript? Easy with the right storage mapping function. Find out how to code a binary tree right up to a depth first traversal.


Other Articles

<ASIN:0137054890>

<ASIN:0596517742>

<ASIN:1590597273>



Last Updated ( Friday, 18 November 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.