Javascript object and its method

Javascript object and its method

ยท

3 min read

Javascript Objects

JavaScript objects are collections of key-value pairs. They are used to store, retrieve, and manipulate data in a structured way. Objects can be created in a number of different ways in JavaScript

Create Objects ( with object literal )

One of the most common ways to create an object in JavaScript is using object literal syntax, which looks like this

const person = {
  name: 'John',
  age: 30,
  occupation: 'developer'
};

In this example, person is an object with three properties: name, age, and occupation. Each property has a key (e.g. name) and a value (e.g. 'John').

you can also use functions in objects, like this

const person = {
  name: 'John',
  age: 30,
  occupation: 'developer',
  printDetails : function (){
    console.log(`${this.name} age is ${this.age} and he is a                                      ${this.occupation}`)
  }
};

Now you wonder what is this this keyword. this keyword just refferce the current object. means if I want to access name under the object then I have access it like this this.name

Access Objects

In this example, person is an object with fore properties: name, age, printDetails and occupation. Each property has a key (e.g. name) and a value (e.g. 'John').

You can access the values of an object's properties using dot notation or square bracket notation. For example:

console.log(person.name); // Output: 'John'
console.log(person['age']); // Output: 30
person.printDetails(); // John age is 30 and he is a developer

Add, Update and Delete properties

You can also add, modify, and delete properties of an object using dot notation or square bracket notation:

// Add properties 
person.email = 'john@example.com';
person['phone'] = '555-555-5555';

// Update properties 
person.age = 31;
person['occupation'] = 'software engineer';

// Delete properties 
delete person.email;
delete person['phone'];

That's all you need to know to work with objects in javascript, but still I am going to show you more two methods to create objects in javascript

Constructor functions

constructor function is a special function that is used to create and initialize an object. Here's an example:

function Person(name, age, occupation) {
  this.name = name;
  this.age = age;
  this.occupation = occupation;
}

const person = new Person('John', 30, 'developer');

In this example, Person is the constructor function and person is the object that is created and initialized by the constructor.

Object.create() method

Finally, you can use the Object.create() method to create an object that is based on another object. This is known as object inheritance. Here's an example:

const personPrototype = {
  sayHello: function() {
    console.log(`Hello, my name is ${this.name} and I am a ${this.occupation}.`);
  }
};

const person = Object.create(personPrototype, {
  name: { value: 'John' },
  age: { value: 30 },
  occupation: { value: 'developer' }
});

Thank you for reading ๐Ÿ˜Š ...

I hope this article will help you in your Javascript journey ๐Ÿ‘๐Ÿ‘

Did you find this article valuable?

Support Rohan Malo's blog by becoming a sponsor. Any amount is appreciated!

ย