Object oriented JS

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Object oriented JS

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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)'
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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/
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply