Page 1 of 1

Object oriented JS

Posted: Tue Feb 28, 2006 7:26 am
by Chris Corbyn
Been working with OOP on JS for a while now writing my own objects and stuff. I've today realised that there's obviously a lot I don't know though. I'm reading over some code that uses syntax like:

Code: Select all

var foo = {
    x: 5,
    y: 10
};
Never seen that before :? When I recurisvely print it I see it's just an object which is how I would do:

Code: Select all

function foo()
{
    this.x = 5;
    this.y = 10;
}

var foo = new foo();
So is the first one just some sort of static class so to speak since it never gets instantiated. Not sure what to google for on this because it's not in any of the tutorials I read about JS OOP.

Also... the code I'm reading contains this:

Code: Select all

this.something = $(something);
What's the $ do? I feel quite left behind here :P

Posted: Tue Feb 28, 2006 8:33 am
by Weirdan
{} - it's called 'object literal'. There are no classes in js, only objects

$ is ordinary function. For example, in Prototype it's used as a shorthand for 'document.getElementById(something)'

Posted: Tue Feb 28, 2006 8:55 am
by Chris Corbyn
Weirdan wrote:{} - it's called 'object literal'. There are no classes in js, only objects

$ is ordinary function. For example, in Prototype it's used as a shorthand for 'document.getElementById(something)'
Ah sweet thanks :) I get that now.

Posted: Tue Feb 28, 2006 9:44 am
by matthijs
Dustin Diaz wrote about object notation recently. Don't know if you have seen this article? http://www.dustindiaz.com/json-for-the-masses/

Posted: Tue Feb 28, 2006 10:32 am
by Chris Corbyn
matthijs wrote:Dustin Diaz wrote about object notation recently. Don't know if you have seen this article? http://www.dustindiaz.com/json-for-the-masses/
You're a legend dude. I haven't read that no but it looks nice and to-the-point :) Thanks.