...

Web Technologies - JAVASCRIPT

Back to Course

Lesson Description


Lession - #209 Javascript Objects


A javaScript object is an substance having state and behavior (properties and javascript object method>
and we cdan check the existence of key in javascript/javascript key in object.
For instance: vehicle, pen, bicycle, seat, glass, keyboard, screen and so forth

JavaScript is an object based language. Everything is an object in JavaScript. and javascript nan is the global object.

JavaScript is template based not class based. Here, we don't make class to get the object. In any case, we direct make objects.

Types to create Objects in javaScript

  • By object literal
  • By creating instance of object directly(using new keyword>
  • By using object constructor(using new keyword>


    Syntax

    
    object={property1:value1,property2:value2,............,propertyN:valueN}
    


    Let's see an example for javascript object

    
    <html>
    <body>
    <script>  
    emp={id:11,name:"Rahul",salary:50000}  
    document.write(emp.id+" "+emp.name+" "+emp.salary>
    ; </script> </body> </html>

    Output


    Try it here

    By creating instance of object directly(using new keyword>

    Syntax

    
    var object_name=new object(>
    ;


    Let's see an example

    
    <html>
    <body>
    <script>  
    var emp=new Object(>
    ; emp.id=11; emp.name="Ravi"; emp.salary=5000; document.write(emp.id+" "+emp.name+" "+emp.salary>
    ; </script> </body> </html>

    Output


    Try it here

    By using object constructor(using new keyword>

    The Object constructor creates an object covering for the given value. Assuming that the value is null or undefined , it will create and return a empty object. Any other way, it will return an object of a Type that relates to the given value. If the value is an object already, it will return the value.


    The this keyword refers to the current object.

    Let's see an example

    
    <html>
    <body>
    <script>  
    function emp(id,name,salary>
    { this.id=id; this.name=name; this.salary=salary; } e=new emp(10,"rohit sharma",20000>
    ; document.write(e.id+" "+e.name+" "+e.salary>
    ; </script> </body> </html>

    Output


    Try it here