JavaScript is a great language that has been taken very seriously in the past couple of years. We can see tons of useful JS libraries on the web for everything, like tweening libraries, MVC frameworks and graphics manipulators. Not to mention the power of Node.js, for real time data exchange and so on.
It's important to understand that nothing changed in the core of the language itself. All the astonishing libs we see in JS today could have been done 5 years ago very well. But at the time there was Flash. ActionScript was the language of choice for many developers when it came to high interactive web apps/websites. Now that the demand is leaving the Flash Player behind, we have to adapt. JavaScript became the answer.
So for those who have been dealing with Object Oriented Programming languages, such as ActionScript 3.0, Java, Python etc. working with a scripting language can be a little effort intense. OOP languages offers us a lot of flexibility and frankly, software development flexibility, is something that started to turn into a need rather than a convenience in the last period.
Well, thankfully, we can exploit the language to give us the flexibility we need when we start thinking about objects, reusability, design patterns – JavaScript as many of you might (and might not) already know, is an almost-entirely object-based language. Its Objects are basically associative arrays augmented with prototypes, nearly just like ActionScript 1.0. If you had the chance to do any AS1 in the past, you should already have a good idea about prototypes, if you didn't, well, just read the rest of the post.
These are some theoretical aspects of the language when it is used to 'simulate' object orientation.
Dynamic Typing:
Types are associated with values rather than variables. It means that a variable bound to a number can be bound to a string later on.
In English:
And it's totally fine.var x = 5;var x = "five"; // WTF?
Functions are Objects:
For that, they have properties and methods. They can be assigned to variables, returned by other functions, passed to arguments and so on.
Prototype-based:
Prototypes are used instead of classes and inheritance. Many class-based features can be simulated with the use of prototype in JavaScript.
Functions can be Object Constructors:
Invoking a function with the new keyword, creates a new object and calls that function with its local this keyword bound to that object for that invocation:
Thevar dude = new Dude();dude.name = "bob";
name property is bound to the dude object and not to the global scope.
Functions can also be methods:
In JavaScript there is no difference between method definitions and function definitions. Rather, the distinction occurs during function calling; a function can be called as a method. When a function is called as a method of an object, the function's local this keyword is bound to that object for that invocation.
These are the points to keep in mind so far. Stay tuned for the next post, where I'll be covering further practical examples, such as writing classes, public/private/static methods, class properties and so on.
"Stay hungry, stay foolish" - Steve Jobs (1955-2011)