Updated the 08/02/2013
I really enjoy to organize my Javascript code as objects and get profit by using inheritance.
“Approaching Javascript as Javascript and not as Java”, i read that so many times; Javascript is / can be OOP and i always thought about OOP as something good for code reusing.
I think the OOP nature of Javascript is all around the prototypal chain; a constructor function is the only way to define objects that achives these two points:
- members on the prototypal chain
- the constructor property and the instance of operator working as intended
What i am going to introduce here is a function Class that uses another syntax to achieve the same, native result than the constructor functions; i am avoiding closures as class pattern because they don’t store members on the prototype (but they are a good way to define private members).
I think that the Class function makes the syntax less redundant and verbose, moving the focus from the function to the prototype. Also, it provides tools for code reusing: Javascript has native inheritance but not native tools to use it.
A more expressive syntax to write objects in OOP approach, as Class function tries to provide, is in my opinion a good way to help the development.
Continue reading →