|Written by Ian Elliot|
|Friday, 30 September 2011|
We noticed the rise of the new star some months ago. Now it's no longer possible to ignore the simple fact that it is everywhere.
You can also see this is a huge advantage. Have you ever seen the way a beginner's eyes glaze over when you start to tell them about objects before they have even written a single line of code? Being able to show how simple algorithms translate to code is a really big advantage and gets them off the launch pad very quickly. Once they are approaching orbit you can start to talk about objects and methods - and they already know about methods as procedural code. What is more after a few medium-sized procedural programs they are usually more than happy to find that there is more to life than one long main program.
alert("some data" 1);
This is perhaps the hardest thing for a traditional object-oriented programmer to come to terms with and most spend a lot of time trying to reinvent class and all of the mechanisms that go with it - like inheritance and a type hierarchy. A classless language doesn't need any of this and a beginner can quite happily just build objects.
The prescription is to just get on with building objects.
An object either has the method and/or property you need or it doesn't. Objects are defined by their collection of properties and this can be changed dynamically. If an object doesn't have a property you can add it or conversely take it away. Objects are dynamic in a very free way. You can think of this as a hierarchy if it suits a particular application but you don't have to if you don't want to. The big problem is that class-based languages spend a lot of effort on building and managing type hierarchies induced by inheritance rules. Similarly class based programmers spend a lot of time working around these hierarchies when it turns out they don't fit. A lot of time can be wasted on pushing a project into a mould that it doesn't fit. You may well want to see a car as "descended" from a more basic vehicle class but you can just as well describe a cars as motorbike with four wheels. Neither description captures the relationship as adequately as the list of properties that they have - some of which are common to both. Type hierarchies force tree structures on general graphs.
This is an amazing trick and it is where the world of modular top-down programming meets all sorts of advanced ideas. As functions are "first class" objects you don't need delegates or function pointers because functions can be passed around just as easily as any other object. Functions can also have properties and methods of their own - and this is perhaps the most mind blowing idea to any programmer schooled in traditional class-based languages.
Yes function can be a complete object that has methods and properties and as such you can use a function to create other object - as a class factory or as a constructor depending on how you want to do things. A constructor has the extra facility of a range of properties that relate to the objects constructed including a prototype chain which can be used to improve the efficiency of object implementation. Notice the prototype chain isn't about inheritance it is about efficiency and the only time you need to use it is if you are planning to create lots of instances of a class. You really can avoid talking about inheritance and all that.
So how does this make it like Basic?
Ok - it does have some faults. But it is important to realize that many of its so called faults are just the result of a programmer trying to make it look like something else. You can't complain the it doesn't have strong typing because this is because it doesn't have classes and this is by design not an oversight.
|Last Updated ( Friday, 30 September 2011 )|