Friday 22 August 2008

Getting intimate with Javascript objects part #8 - The Inherited Methods and Properties

Gentle reader,
Back to the wonderful geeky world of Javascript , it has been a while since the last post in this series, I do apologise.. here is a link to the thread in case you missed the excitement that was parts 1- 7,

In this post I am going to look at the sometimes overlooked methods and properties that come as part an parcel of every object you create in JS. As I mentioned before every object you create is a "child" of the Object class and as such in inherits all the parent's methods and properties. Lets look at a few :-

toString() this method does not require any parameters and returns a string that will in some way represent the the type and value of the object. I am sorry that sounds a bit vague, but have a play with it on your own objects and you will see what I mean. You will probably get something like [object Object] which is really not that wonderful. However if you are debugging some code and for the life of you, you can'rt remember what class constructor was used to create and object you can use the toString method to test this, for example if the constructor of an object was the Array class, toString will return [object Array] as a string. This will only work for classes that are part of the JS implementation of your browser. If you try this on your own classes you will always get [object Object]. What the JS interpreter gives the JS interpreter takes away ~sigh~.

Given that it already exists you can create your own customer toString using the prototype defintion, something like this.

GirlFriend.prototype.toString = function() {
return "[The name of this girlfriend is "+" and she has "+this.hairColor+"]";

Which brings me to something I should have mentioned before ,when we were discussing prototypes. You can still access the original method using the apply(0) method. for example


[Note the 0 is a zero!]

toLocalString() is the next method and it does the exactly the same as toString() except for objects created with the ARRAY,DATE and NUMBER classes which do return values based on the Locale of the client.

valueOf() is like toString() except that it returns a primitive value that is NOT a string. Oddly since a object is an object and seldom has a primitive value this method will simply return the object. However if the object is created from the NUMBER and BOOLEAN have primitive values so these are returned when valueOf() is called.

hasOwnProperty() this can be used to test an object for the possession of a property that was NOT inherited from a parent class. It returns either true or false. For example
GirlFriend.hasOwnProperty("toString") would return false as toString is inherited from the Object Class.
Where as
GirlFriend.hasOwnProperty("hair") would return true because hair is not inherited but defined within the GirlFriend class

propertyIsEnumerable() which is a bit of a mouthful and returns true if you can put the property being tested in a for/in loop, false if not. But beware if the property is inherited then this method always returns false!

isPrototypeOf() this returns true if the object is the prototype object of the object passed to the function. Otherwise it returns false. for example

var GF = new GirlFriend();
GirlFriend.prototype.isPrototypeOf(GF) will return true
GirFriend.isPrototypeOf(GF) will return false

Anyway thats really all I have to say about Objects for the now. So this Thread is closed ;-)

Sometime soon, after my holidays I will take a wander in to the wonderfully indexed world of Arrays!

Disqus for Domi-No-Yes-Maybe