Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Objects

JS Objects

You have already learned that JavaScript variables are containers for data values. Objects are variables too. But objects can contain many values.

The values are written as name:value pairs (name and value separated by a colon).

It is a common practice to declare objects with the const keyword.

					 
        
          const user = {name:"John Doe", email:"john@gmail.com"};
        
      
Object Definition

You define (and create) a JavaScript object with an object literal.

					 
        
          const user1 = {name:"John Doe", email:"john@gmail.com"};

          const user2 = {
                name:"John Doe",
                email:"john@gmail.com"
              };
        
      
Object Properties

The name:values pairs in JavaScript objects are called properties.

Accessing Object Properties

You can access object properties in two ways:

					 
        
          objectName.propertyName
          // or
          objectName["propertyName"]

          user.name // John Doe
          user["email"] // john@gmail.com
        
      
Object Methods

Objects can also have methods (or functions).

Methods are actions that can be performed on objects.

Methods are stored in properties as function definitions.

A method is a function stored as a property.

					 
        
          const user = {
                firstName:"John",
                lastName:"Doe",
                email:"john@gmail.com",
                fullName : function() {
                  return this.firstName + " " + this.lastName;
                }
              };

         // Accessing object methods
         user.fullName(); // John Doe
         user.fullName; // function definition itself
        
      
What is this?

In JavaScript, the this keyword refers to an object.

Which object depends on how this is being invoked (used or called).

The this keyword refers to different objects depending on how it is used

Alone, this refers to the global object.

In a function, this refers to the global object.

In an event, this refers to the element that received the event.

Methods like call(), apply(), and bind() can refer this to any object.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved