JavaScript Objects


What is an object in JavaScript?

An object in JavaScript is a collection of key-value pairs. Each key is a string (or Symbol), and the value can be any data type, including other objects. Objects are used to store and manage data in a structured way.


How do you create an object in JavaScript?

There are several ways to create objects in JavaScript:

  • Object Literal:

  const person = {
    name: 'Alice',
    age: 30
  };
  
  • Using the new Keyword:

  const person = new Object();
  person.name = 'Bob';
  person.age = 25;
  
  • Using a Constructor Function:

  function Person(name, age) {
    this.name = name;
    this.age = age;
  }
  const person1 = new Person('Charlie', 28);
  
  • Using Object.create():

  const proto = { greet: function() { console.log(`Hello, ${this.name}`); } };
  const person = Object.create(proto);
  person.name = 'Diana';
  

How do you access object properties?

You can access object properties using dot notation or bracket notation.


const person = {
  name: 'Alice',
  age: 30
};

console.log(person.name); // Dot notation: Alice
console.log(person['age']); // Bracket notation: 30

What are methods in JavaScript objects?

Methods are functions that are properties of an object. They are used to define behaviors for the object.


const person = {
  name: 'Alice',
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

person.greet(); // Hello, my name is Alice

What is the this keyword in the context of an object?

The this keyword refers to the context in which a function is called. Inside a method, this refers to the object the method is called on.


const person = {
  name: 'Alice',
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

person.greet(); // Hello, my name is Alice

How do you add, update, and delete properties of an object?

You can add or update properties using dot notation or bracket notation, and you can delete properties using the delete operator.


const person = { name: 'Alice', age: 30 };

// Adding a property
person.city = 'New York';

// Updating a property
person.age = 31;

// Deleting a property
delete person.city;

console.log(person); // { name: 'Alice', age: 31 }

What is the difference between shallow copy and deep copy of objects?

A shallow copy creates a new object with the same properties as the original object, but nested objects are still referenced. A deep copy creates a new object along with copies of nested objects, meaning changes to the copied object do not affect the original.


// Shallow copy
const original = { a: 1, b: { c: 2 } };
const shallowCopy = { ...original };
shallowCopy.b.c = 3;
console.log(original.b.c); // 3 (affected)

// Deep copy using JSON methods
const deepCopy = JSON.parse(JSON.stringify(original));
deepCopy.b.c = 4;
console.log(original.b.c); // 3 (not affected)

What are computed property names in JavaScript objects?

Computed property names allow you to use an expression in brackets to define the property name when creating an object. This is useful when the property name is dynamic.


const propName = 'age';
const person = {
  name: 'Alice',
  [propName]: 30 // Computed property name
};

console.log(person.age); // 30

What is an object method and how do you define one?

An object method is a function that is a property of an object. You define a method by assigning a function to a property of the object.


const car = {
  make: 'Toyota',
  model: 'Camry',
  start: function() {
    console.log('Car started');
  }
};

car.start(); // Car started

How do you check if a property exists in an object?

You can check if a property exists in an object using the in operator or by checking if the property is defined using the hasOwnProperty() method.


const person = { name: 'Alice', age: 30 };

console.log('name' in person); // true
console.log(person.hasOwnProperty('age')); // true

What is the prototype of an object?

The prototype of an object is an object from which it inherits properties and methods. Every JavaScript object has a prototype, and the prototype can be used to share methods and properties among all instances of a given type.


// Defining a constructor function
function Animal(name) {
  this.name = name;
}

// Adding a method to the prototype
Animal.prototype.speak = function() {
  console.log(`${this.name} makes a noise.`);
};

const dog = new Animal('Dog');
dog.speak(); // Dog makes a noise.

How do you create a new object using a constructor function?

You can create a new object using a constructor function by using the new keyword, which creates an instance of the object and sets up the prototype chain.


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

const person1 = new Person('Alice', 30);
console.log(person1.name); // Alice
Ads