Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Code Dragon

Code Dragon Avatar
Never raise more devils than you can slay.

*
New Member

17


February 2007
Okay, there are very few tutorials on this, so now I'm going to write one on it. :P

Basically, it's just simple JavaScript Objects, but in shorthand notation, hence "JavaScript Shorthand Object Notation."

First thing's first: we have to declare an object.

var newObj = {}

Now, you can put everything you need to inside of that object. Let's define two integers as part of the object:

var newObj = {
int1:5,
int2:6
}

Notice, that nowhere did I use a semicolon. You have to separate each part of the object with a comma (,). Now let's do some math with our new object.

var newObj = {
int1:5,
int2:6
}
document.write(newObj.int1+newObj.int2);

See how I put newObj in front of it? The object name is the prefix for any of its containing elements, if they are called from outside of the object. Now let's add the function inside of the object.

var newObj = {
int1:5,
int2:6,
addInts:function(){return this.int1+this.int2;}
}

Okay, so first, we declared the addInts part of our object as a function, and inside of this function, we make it return int1 plus int2. However, since it's inside of the object, it can't be called as newObj.int1. Instead, you can use the this constructor, to specify that you want to use the current object. Now to call the function.

var newObj = {
int1:5,
int2:6,
addInts:function(){
return (this.int1+this.int2);
}
}
newObj.addInts();

See? It's that simple. You can make your scripts look complicated like this, too. Anyway, notice that I encased the addition within parentheses. This evaluates the expression before it's returned.

You can also pass parameters into the function(){} definition.

var newObj = {
addInts:function(int1,int2){
return (int1+int2);
}
}
newObj.addInts(5,6);

Simple, right? Well, that's basically all there is to JSON. It's that simple. :)


Last Edit: Mar 9, 2007 0:12:05 GMT by Code Dragon

A forum for Web artists and chao breeders from around the world. Spriters, Animators, Coders, and general people. You can come to Chao Talk, and be part of something. Here, YOU can make a difference.

newBookmarkLockedFalling