Monday, 20 May 2013

Javascript Objects

What would you say an Object is? An object is as the name implies. It is something with certain specifications, for example if you take a person. You have a name, surname, age, weight, length and so on. All of these are concidered a person's properties.


Now that we have concidered person as an object, the next part is to created your object:
Now that we have our object, we can start setting values. But as you will now see, you only need to specify a property name, and a value. The property will be created in the object with little effort.

Take this for example:


You have to clerify which object you're using, which is person. Seeing that we want to set properties for this object, you have to add a ., meaning you will go inside the object. thus we have ( person. ). Because we want to set the property name of person to Phillip for example, you do this as shown above.

After all the properties has been set, will should have the following:
Now we have created our object and set the properties. Now what we want to do is to return the values which have been stored inside the object. This is the same method as storing data inside the object. Let's say we want the person's name.
We will use



to access the name field inside of person.

Now I will display the name and Surname of the person object using:


The ouput for this is:
Thus we are able to access the properties of the given object. These propertiy values can be stored in variables if necessary.

Next we will have a look at Custom Objects:


No comments:

Post a Comment