Nov

27

There are three main object-oriented approaches JavaScript developers use to build application: constructor (singleton class) approach, prototype approach and JSON approach.

Constructor- A constructor in JavaScript is actually nothing more than a function. This function also serves as the constructor of the class.

Prototype - Every single object in JavaScript has a prototype property associated with it. There is no real equivalent to prototype in any other major language(except ActionScript, but same as JavaScript, ActionScript is also based on ECMAScript).

JSON - JSON stands for JavaScript Object Notation, people who do mashups know that JSON’s main application is in Ajax web application programming, it serves as an alternative to XML format to represent nested data and associative array.

You may ask, which is the best approach? The answer is that they are all the best at what they do.

Constructor approach is the easiest to comprehend, as it is used by many other major programming languages. One good example mainly bulit using this approach  is YUI (Yahoo! User Interface), when you take a look at the code, it’s not hard to find that it is the most logical and the most similar to the more fully object-oriented languages.


I’d like to use prototype approach when creating a class that is large and I know there may be multiple instances of it, this will lead to the best memory efficiency, which is always an important goal. One good example bulit using this approach will be Prototype JavaScript framework - a library provides functionality that is similiar to YUI.

The JSON approach is probably a good choice if your object hierarchy is going to be highly nested. As I said earlier, it is a good alternative to XML, instead of reading an XML file as XML and parsing it via the DOM or reading it as text and using regular expressions, it would be a lot easier and less straining to the system to have the data in a format that JavaScript can use directly. I doubt there are many easier ways than JSON for this, and that is in no small part because that’s largely what it was designed for! If you do a lot of mashups, you should know many RSS Feeds and REST APIs has JSON format contents ready for developers to mashup on the fly.

For detailed comparison of constructor and prototype paradigm, please feel free to check out one of my another post -  Constructor vs Prototype.



Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

Sponsors




Links