View Full Version : Robust Custom Object Construction question

Mar 15th, 2007, 08:04 AM
I would like to construct object classes with properties and methods that I define. I'd like to be able to use these with a syntax similar to that of the pre-constructed javascript objects. For example, if I have an object foo and I define a property bar, I'd like to be able create an instance of foo like this:
var x = new foo... and I'd like to get the property bar for this instance like this:
var y = x.bar... and set the property using this:
x.bar = y
Is this even possible in javascript? And if so, could someone either provide me with a simple example of how it's done, or better yet, a decent reference or tutorial written in English (not CompSci Jargon)?

Thanks much for any help.

Mar 15th, 2007, 08:32 AM
There are several ways to create an object (with it's properties/methods) in javascript. One of them is JSON (see http://www.json.org/ and http://www.json.org/js.html)

<script type="text/javascript">
var x={

javascript is not a class oriented language, it is a prototype oriented language (thus it has no classes, not so far in common versions).

You may also create objects using the prototype object:

Both methods are robust enough, crossbrowser and stable.