javaScript objects

an object is an unordered collection of data, including primitive types, functions, and even other objects.

an object is created with a constructor, a special type of function that prepares a new object for use by initializing the memory it takes up.

everything in javascript is an object: a number, a string, an array ...

an object is a collection that can contain properties and methods.

properties are values associated with objects.

methods are actions that objects can perform.


how create the javaScript objects

the javascript object create using new operator.

this below example creates an Object called 'person' and adds properties to it:

      <script>
 
            var     person    =  new  object();
            
                     person.firstname  ="javascript object";
                   
                     person.lastname   ="ecma script";
                   
                     person.age        =23;
                   
                     person.salary     =100000;
                   
      </script>

in javascript objects can also be created with the object notation, which uses curly braces.

         var   aa = {};

inside the braces the object's properties are defined as name and value pairs(name:value).the properties are separated by commas:

         var  person = {
                                 name :"name property value",
                      
                                 lastname:"for javascript object",
                      
                                 age:"20000"
                   
                            };

JavaScript - accessing object properties

properties are the values associated with an object.

the syntax for accessing the property of an object is:

  objectname.propertyname ;

the example uses the 'firstname', 'lastname' property of the 'person' object to access property values:

               
                 var   name = person.firstname;
                 
                 var   last   = person.lastname;

accessing object properties with array syntax

in this syntax the dot operator is the array ([]) operator.

it can be more powerful than the dot-operator syntax.

  objectname["propertyname"];

javaScript - accessing object methods

you can call a method with the following syntax:

  objectname.methodname()
  objectname["methodname"]()

the below example uses the length() method of the string object, to count the number of characters:

           var    name  =  new   string("javascript object");
           
           var    x     =  name.length();
           
           document.write("the length of string = "+x);

objects in javascript fall into four groups:

  • user defined objects are custom objects created by the programmer to bring structure and consistency to a particular programming tast.
  • built-in objects are provided by the javascript language itself.these include those objects associated with the types (string, number, boolean), objects that allow creation of user-defined objects and composite types (object, array), and objects that simplify common tasks, such as date, math, regexp.the capabilities of built-in objects are governed by the ecma-262 language standard.
  • browser objects are those objects not specified as part of the javascript language but that most browser commonly support.for example the browser objects include window object, navigator object.
  • document objects are part of the document object model(dom),as defined by the w3c.these objects present the programmer with a structured interface to xhtml and xml documents.access to the document objects is provided by the browser via the document property of the window object (window.document).

note :- javascript language hides memory management issues from programmers.when you create an object in javascript, the interpreter invisibly allocates memory for you to use.

garbage collection in javascript keep a watchfull eye on your data.when a piece of data is no longer accessible to your program, the space it occupies is reclaimed by the interpreter and returned to the pool of available memory.