I have been using Java for a long time, and for quite some time I was doing web development using GWT (Google Web Toolkit). The beauty of it was that I had my Java object oriented constructs and would not worry about how it gets translated to GWT - let Google take care of it. My knowledge of Javascript was sufficient but not to the extent that I could do heavy web development with it. Later I decided that I have to get a deeper and more thorough understanding of Javascript and this has been a real roller coaster - just at a time where I would think I got something, something came and proved me that I was wrong - that I simply misunderstood.
The culprit for me - Javascript's flexibility, or should I say over-flexibility. Bees that have honey in their mouths, have stings in their tails and he who will gather honey must bear the sting of the bees. Javascript's flexibility is like a wild horse - you have to understand it, so you can play with it. What one needs in Javascript is strong disciplined code conventions and constructs. What I needed from Javascript - a set of code conventions and constructs that will mimic the most popular Java OO concepts.
This post is the first of a series of posts to come and it will cover Custom Objects in Javascript.
In an Object Oriented Programming language like Java, an object is an instance of a class. The class defines member variables and methods that operate on that data. Objects represent instances of the class, and a class represents a blueprint of how a new object is created. In Javascript, this is not the case. Javascript is a prototype-based language and there is no class statement. So, how do we mimick the class behaviour popular in object oriented programming languages like Java? How do we instantiate a new object in Javascript? What is the equivalent of the
The culprit for me - Javascript's flexibility, or should I say over-flexibility. Bees that have honey in their mouths, have stings in their tails and he who will gather honey must bear the sting of the bees. Javascript's flexibility is like a wild horse - you have to understand it, so you can play with it. What one needs in Javascript is strong disciplined code conventions and constructs. What I needed from Javascript - a set of code conventions and constructs that will mimic the most popular Java OO concepts.
This post is the first of a series of posts to come and it will cover Custom Objects in Javascript.
In an Object Oriented Programming language like Java, an object is an instance of a class. The class defines member variables and methods that operate on that data. Objects represent instances of the class, and a class represents a blueprint of how a new object is created. In Javascript, this is not the case. Javascript is a prototype-based language and there is no class statement. So, how do we mimick the class behaviour popular in object oriented programming languages like Java? How do we instantiate a new object in Javascript? What is the equivalent of the
new
keyword in Javascript?