Object – Oriented Javascript

by

A lot of people (including myself, until recently!) think of that JavaScript is the language, they’re good at, just because they know C/C++/C# or they have some prior programming knowledge. This experience is not incorrect because it is easy to do simple things with JavaScript or to say with even little programming knowledge, we can start working with JavaScript. As the complexity and interactivity of this new generation of Web applications increasing, writing JavaScript code requires an entirely different approach.Recently, I have come across an Article on MSDN Magazine, which discuss about Object Oriented JavaScript and how one can use this support to do object-oriented development effectively in JavaScript. I found this concept very useful and thought to discuss with all. 

Object-oriented programming (OOP) is one popular approach that is used in many JavaScript libraries to make a codebase more manageable and maintainable. JavaScript supports OOP, but it does so in a very different manner from the way popular Microsoft .NET Framework compliant languages like C++, C#, or Visual Basic do it.

 In C++ or C#, we use objects, we are referring to instances of classes. Objects have different properties and methods, depending on the class they are instantiated from. That is not the case with JavaScript objects. In JavaScript, objects are just collections of name/value pair, we can compare JavaScript object as a dictionary with string keys. We can get and set the properties of an object using either the familiar “.” (dot) operator, or the “[]” operator, which is typically used when dealing with a dictionary. The following snippet

var UserObject = new Object();UserObject.lastLoginTime = new Date(); 

alert(UserObject.lastLoginTime); 

does exactly the same thing as this: 

var UserObject = {};  // equivalent to new Object();

UserObject [“lastLoginTime”] = new Date();

alert(UserObject[“lastLoginTime”]); 

We can also define the lastLoginTime property directly within UserObject’s definition like this: 

Var UserObject = {“lastLoginTime” : new Date() };

alert(UserObject.lastLoginTime); 

These examples also show how much more malleable JavaScript objects are than C++ or C# objects. Property lastLoginTime doesn’t have to be declared beforehand—if UserObject doesn’t have a property by that name, it will simply be added to UserObject. Again, I compare here a JavaScript object is a dictionary — after all, we add new keys (and their respective values) to dictionaries all the time.

So, there we have object properties. Likewise it has object methods too and there is also a lot many things to know about Object Oriented JavaScript. Soon I will come up with them……… 

Advertisement

One Response to “Object – Oriented Javascript”

  1. gauravsharma82 Says:

    Good information Harleen. I always get confused with JS. :) I blame it simply on lack of experience.

    Can you elaborate on scenerios when you will use JS like this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.