Monday 14 July 2008

Getting intimate with Javascript objects part #5 - Instance and Class Methods and Properties

A while ago I started this thread and in the last post i said that "...tomorrow I will look at Instance Properties,Instance Methods,Class properties and Class methods" Well my only excuse is that I didn't tell you which tomorrow it was going to be.

Apologies for the hiatus, I was caught up in other things.... anyway on with the show.

Instance properties
Every objedct has its own separate copies of its instance properties. Lets say I have this code

var StevesGirlfriend = new GirlFriend();
var NiallsGirlFirend = new GirlFriend();
var PaulsGirlFriend = new GirlFriend()'

StevesGirlFriend.hair = "Blonde";
NiallsGirlFriend.hair = "Brunette";
PaulsGirlFriend.hair = "Ginger";

I have 3 objects all with an instance property called hair and each object has its own instance property called hair. By default any object property in Javascript is an instance property, however if we are going to be all anal about the Object Orientedness we have to say that instance properties are those that are created or initialized inside an object by the constructor method.

Instance Methods
Instance Methods differ from Instance Properties only in that they contain methods (or if you prefer "code to do shit with") rather than a data value. Like the Instance property each object has its own private copy of the method. So when you run this code

var StevesGirlfriend = new GirlFriend();
var NiallsGirlFirend = new GirlFriend();
var PaulsGirlFriend = new GirlFriend()'

StevesGirlFriend.hair = "Blonde";
NiallsGirlFriend.hair = "Brunette";
PaulsGirlFriend.hair = "Ginger";

StevesGirlFriend.kiss("no tongues");


The method kiss("no tongues") is run only with the StevesGirlFriend object and is not run for any of the other objects. Easy init? :-)

Class Properties

Now pay close attention ... this is where it gets a little freaky ...
Class Properties are properties that are aassociated with the class that defines the object rather than any object that is created from that class, no matter how many that may be.

We do this by creating and initializing the property in the constructor method ... for example

GirlFriend.gender = "Female";

The code above is the constructor method for the GirlFriend object. When new Girlfriend() is called it is created and the class property GirlFriend.gender = "Female" Now whenever the gender of the object is accesses the Class Property is retrieved and not the Instance Property

Class Methods

A class method is a method associated with a class rather than an instance of a class.

If you have used Javascript before you will have come across these already, for example the Date.parse() method is always invoked through the class constructor method rather than through a particular instance of the class.

**WARNING**
because class methods are bound to the class and not an instance of the class class methods cannot use the this keyword!

Putting all that together

OK

function GirlFriend()
{
// This is an Instance variable
this.hair = "blonde";
}
// This is a Class Property
GirlFriend.gender = "Female";

// This is a function and as it is a prototype that makes it an Instance Method
GirlFriend.prototype.kiss = { return "Muahhhhhhhhh" };

// This function compares two GirlFriend objects and makes a sexist comment
// Since this function has to compare two instances of the same class it doesnt
// make sense to have it associated with each instance. So I make it a class method.
GirlFirend.compare(gfa,gfb) {
if (gfa.hair == "blonde")

{
alert("Girlfriend A is a Blonde Bombshell")
}
if (gfb.hair == "blonde")
{
alert("Girlfriend B is a Blonde Bombshell")
}
}

Hopefully that should make sense :-)

Next post will be on Superclasses and Subclasses

Disqus for Domi-No-Yes-Maybe