php john green

Object Oriented JavaScript tutorial

Creating the Object

javascript object literal

In this script we create an object called John when we call the function newPerson. However the property John.name of the object John has no value set yet.

We can set this property by either calling John.Person.setName("John"); or we could also do this directly by setting John.Person.name = "John"; The preferred method is to use the method setName although the reality is that as this variable is not encapsulated we could do either.

web developer hire

We can also create another object thusly: Sally = new newPerson(); and set the name of this object to sally as above by calling Sally. Person.setName("Sally");

If we then decided to kill off poor Sally, we could call : Sally.Person.killPerson(); If we then set foobar = Sally.Person.getStatus(); foobar would be a variable that would be a string that contains the phrase "this person is dead".

Calling an Object

If we deleted the line John.Person.fines = new John.Person.fines(); then the line var johnsFines = John.Person.fines.fine.getFines(); would give us an undefined variable as the object would not be accessible without the constructor call first.

Feel free to copy and paste the code above into Firefox or your favorite debugger and play with the script, but if you decide to reuse this code snippet please give credit where credit is due and put my name in there somewhere and link back to this article.

Check back regularly as this website is always being updated and tutorials being added.

<< Previous Page | Start again > >

Article by J C Green

This article was written for non-profit educational purposes. Comments? Suggestions? Rants or raves? E-mail me using the form on this website.