Share
Facebook
Twitter
LinkedIn
Whatsapp
Instagram
Messenger

জাভাস্ক্রিপ্ট


JavaScript is a versatile language that uses objects extensively. Understanding how objects work is crucial for writing effective and efficient JavaScript code. In this tutorial, we'll cover the basics of JavaScript objects, including their creation, properties, methods, and advanced features like prototypes and inheritance.


Introduction to JavaScript Objects

In JavaScript, objects are collections of key-value pairs. The keys are strings (or symbols), and the values can be any data type, including other objects. Objects are fundamental to JavaScript, enabling you to group related data and functionality.


Creating Objects

Object Literals The most common way to create an object is using an object literal. Here’s a basic example:

উদাহরণ


const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
};

কোড এডিটর


In this example, person is an object with three properties: firstName, lastName, and age.

Constructor Functions
You can also create objects using constructor functions. These functions are defined with the function keyword and use the new operator to create instances.

উদাহরণ


function Person(firstName, lastName, age) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
}

const john = new Person('John', 'Doe', 30);

কোড এডিটর


ES6 Classes With ES6, JavaScript introduced classes, which provide a more syntactic sugar over constructor functions.

উদাহরণ


class Person {
  constructor(firstName, lastName, age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
  }
}

const john = new Person('John', 'Doe', 30);

কোড এডিটর



Accessing and Modifying Object Properties

Dot Notation
You can access and modify object properties using dot notation.

উদাহরণ


console.log(person.firstName); // Output: John
person.age = 31;
console.log(person.age); // Output: 31

কোড এডিটর


Bracket Notation Bracket notation is useful when property names are dynamic or not valid identifiers.

উদাহরণ


console.log(person['lastName']); // Output: Doe
person['age'] = 32;
console.log(person['age']); // Output: 32

কোড এডিটর



Methods in Objects

Objects can also have methods, which are functions associated with the object.

উদাহরণ


const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
  fullName: function() {
    return `${this.firstName} ${this.lastName}`;
  }
};

console.log(person.fullName()); // Output: John Doe

কোড এডিটর



Prototypes and Inheritance

JavaScript uses prototypes for inheritance. Every object in JavaScript has a prototype object from which it inherits properties and methods.

Prototype Chain
You can add methods and properties to an object's prototype to be shared among all instances.

উদাহরণ


Person.prototype.greet = function() {
  return `Hello, my name is ${this.firstName} ${this.lastName}.`;
};

const john = new Person('John', 'Doe', 30);
console.log(john.greet()); // Output: Hello, my name is John Doe.

কোড এডিটর


Inheritance
You can create a hierarchy of objects using prototypes.

উদাহরণ


class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks.`);
  }
}

const dog = new Dog('Rex');
dog.speak(); // Output: Rex barks.

কোড এডিটর



Object Destructuring

Object destructuring allows you to extract values from objects into variables.

উদাহরণ


const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
};

const { firstName, age } = person;
console.log(firstName); // Output: John
console.log(age); // Output: 30

কোড এডিটর



Object Spread and Rest

The spread operator (...) can be used to create shallow copies of objects or merge multiple objects.

উদাহরণ


const person = { firstName: 'John', lastName: 'Doe' };
const address = { city: 'New York', zip: '10001' };

const personWithAddress = { ...person, ...address };
console.log(personWithAddress);
// Output: { firstName: 'John', lastName: 'Doe', city: 'New York', zip: '10001' }

কোড এডিটর



Object Methods: Object.keys(), Object.values(), and Object.entries()

These methods are useful for interacting with objects.

<

উদাহরণ


const person = { firstName: 'John', lastName: 'Doe', age: 30 };

console.log(Object.keys(person));   // Output: ['firstName', 'lastName', 'age']
console.log(Object.values(person)); // Output: ['John', 'Doe', 30]
console.log(Object.entries(person)); // Output: [['firstName', 'John'], ['lastName', 'Doe'], ['age', 30]]

কোড এডিটর



Object Methods: Object.keys(), Object.values(), and Object.entries()

These methods are useful for interacting with objects.

<

উদাহরণ


const person = { firstName: 'John', lastName: 'Doe', age: 30 };

console.log(Object.keys(person));   // Output: ['firstName', 'lastName', 'age']
console.log(Object.values(person)); // Output: ['John', 'Doe', 30]
console.log(Object.entries(person)); // Output: [['firstName', 'John'], ['lastName', 'Doe'], ['age', 30]]

কোড এডিটর



Conclusion

JavaScript objects are a fundamental concept in the language, enabling you to store and manage data efficiently. From basic object creation to advanced topics like prototypes and inheritance, understanding how to work with objects will significantly enhance your JavaScript programming skills. Practice these concepts by creating and manipulating objects in different ways to gain a deeper understanding.