Object Syntax in JavaScript

JavaScript Object Syntax

 
In this article, I’ll explain objects in JavaScript.
  • Objects are composite data types that are built from primitive and other objects.
  • An object’s building blocks are commonly referred to as its properties.
  • Properties are used to describe some aspect of an object.
  • For example, a property can describe the color of a horse.
Creating Objects
  • The language provides syntax known as object literal notation for quickly creating objects.
  • Object literals are denoted by curly braces.
    For example:
    1. var object = {};  
  • Inside of the curly braces, properties and their values are specified as a list of key/value pairs. 
     
    • The following example creates an object with three properties using literal notation.
    • The first property,abhi, holds the number ten.
    • The second property, jeet, is specified using a string, and also stores a string value.
    • The third property, bittoo, stores an empty object.
      1. var object = { abhi: 10,  
      2.         "jeet""hello world..",  
      3.          bittoo: {}  
      4.  }; 
Accessing Properties
  1. JavaScript provides two notations for accessing object properties.
     
    • Dot Notation
    • Bracket Notation
       
  2. The first, and most common, is known as dot notation.
  3. Under dot notation, a property is accessed by giving the host object’s name, followed by a period (or dot), followed by the property name.
  4. If object.abhi initially held the value ten, then its value would become eleven after executing this statement.
  5. Note that if an object.abhi did not already have a value, then it would be undefined.
    1. object.abhi = object.abhi+ 1;
  6. The alternate syntax for accessing object properties is known as bracket notation.
  7. In bracket notation, the object name is followed by a set of square brackets. Inside the square brackets, the property name is specified as a string.
     
    For example
    1. object["abhi"] = object["abhi"] + 1; 
Accessing Nested Properties
  • Properties of nested objects can be accessed by chaining dot and/or bracket references together.
     
    For example, the following object contains a nested object named bittoo, that contains another object named abhi, that has a property named jeet that holds the value six.
    1. var object = {  
    2.     bittoo: {  
    3.              abhi: {  
    4.              jeet: 6  
    5.        }  
    6.    }  
    7. }; 
  • The following expressions access the nested property, jeet.
     
    • The first expression uses dot notation,
    • The second expression uses square bracket notation.
    • The third expression combines both notations to do the same result.
      1. object.bittoo.abhi.jeet;  
      2. object["bittoo"]["abhi"]["jeet"];  
      3. object["bittoo"].abhi["jeet"];    
Functions as Methods
  • When a function is used as an object property, it is called a method.
  • Like properties, methods can also be specified in object literal notation.
     
    For example
    1. var object = {  
    2.         sum: function (abhi, jeet) {  
    3.               return abhi + jeet;  
    4.        }  
    5.  };    
  • Methods can also be invoked using dot and bracket notation.
     
    For example
    1. object.sum(10, 20);  
    2. object["sum"](10, 20);