Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted November 19th, 2015
JavaScript: Objects, Properties and Methods

JavaScript Objects can be compared to real life objects. For example a mobile phone is an object that got its own property. Think of a property like a category label that belongs to some object. Properties of the mobilePhone can be: model, color and OS. Or consider an object as a list that got its own items.

On the example below model is a property and followed by : and then iPhone 6 as the value of the property. To add more properties it should be separated by commas.

Example:


   var mobilePhone = {
        model: "iPhone 6",
        color: "Space Gray",
        OS: "iOS 9.1"
   };

Object also have methods it is similar to function , that is associated with an object. It can be use to change the property value of an object.
On the example below, instead of having a property like model and color, I put a method called setColor. In this method you can give the mobilePhone a different color, I set the color to “White.”
To change the color of the iPhone to “White”, we do it by calling mobilePhone.setColor("White").
The difference to define a method is to put it in the function after the equal sign.

Example:


    var mobilePhone = {
        model: "iPhone 6",
        color: "Space Gray",
        OS: "iOS 9.1"
   };
    mobilePhone.setColor = function (newColor){
        mobilePhone.age = newAge;
    };
    mobilePhone.setColor("White");

Posted by Valerie Varias

Leave a Reply

Your email address will not be published. Required fields are marked *