Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JavaScript objects

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 103
    Comment on it

    A javaScript object is an entity having state and behavior. For example: car, pen, bike, chair, glass, keyboard, monitor etc. Objects are real world entity.
    JavaScript is an object-based language. Everything is an object in JavaScript.

    There are 3 ways to create objects.

    1)By object literal
    2)By creating instance of Object directly using new keyword
    3)By using an object constructor using new keyword


    1)The syntax of creating object using object literal is given below:

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



    Below is the simple example of creating object in JavaScript.

    <html>
    <body>
    <script>  
    emp={id:102,name:"Pranav Chhabra",salary:40000}  
    document.write(emp.id+" "+emp.name+" "+emp.salary);  
    </script>
    </body>
    </html>
    


    2) The syntax of creating object directly is given below:
    var objectname=new Object();

    Below is the example of creating object directly

    <html>
    <body>
    <script>  
    var emp=new Object();  
    emp.id=101;  
    emp.name="Pranav Chhabra";  
    emp.salary=50000;  
    document.write(emp.id+" "+emp.name+" "+emp.salary);  
    </script> 
    </body>
    </html>
    

    3) By using an Object constructor.
    Here, you need to create function with arguments.

    <html>
    <body>
    <script>  
    function emp(id,name,salary){  
    this.id=id;  
    this.name=name;  
    this.salary=salary;  
    }  
    e=new emp(103,"Pranav Chhabra",30000);  
    
    document.write(e.id+" "+e.name+" "+e.salary);  
    </script>  
    </body>
    </html>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: