JavaScript Objects and it's Methods

JavaScript Objects and it's Methods

JavaScript Objects

JavaScript objects are collections of key-value pairs. The values can be any data type, including arrays and other objects. Objects in JavaScript are used to represent real-world entities, such as a user or a product.

Creating Objects

There are several ways to create an object in JavaScript. The most common method is using the Object constructor:

const person = new Object();
person.name = 'John';
person.age = 30;
person.greet = function() {
  console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
};

person.greet(); // Output: "Hello, my name is John and I am 30 years old."

Another way to create an object is by using object literal notation:

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
};

person.greet(); // Output: "Hello, my name is John and I am 30 years old."

Accessing and Modifying Object Properties

There are several ways to access and modify object properties in JavaScript. The most common method is using dot notation:

Copy codeconst person = {
  name: 'John',
  age: 30
};

console.log(person.name); // Output: "John"

person.name = 'Jane';
console.log(person.name); // Output: "Jane"

You can also use bracket notation to access and modify object properties:

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

console.log(person['name']); // Output: "John"

person['name'] = 'Jane';
console.log(person['name']); // Output: "Jane"

Bracket notation is useful when the property name is stored in a variable:

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

const property = 'name';
console.log(person[property]); // Output: "John"

person[property] = 'Jane';
console.log(person[property]); // Output: "Jane"

Object Methods

Objects can have functions as their values, which are called methods. To call a method, use the dot notation or the bracket notation followed by parentheses:

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
};

person.greet(); // Output: "Hello, my name is John and I am 30 years old."
person['greet'](); // Output: "Hello, my name is John and I am 30 years old."

Enumerating Object Properties

You can use the for...in loop to enumerate the properties of an object:

const person = {
  name: 'John',
  age: 30,
  greet: function() {
console.log(Hello, my name is ${this.name} and I am ${this.age} years old.);
}
};

for (const key in person) {
console.log(${key}: ${person[key]});
}

// Output:
// "name: John"
// "age: 30"
// "greet: function() {...}"

Note that the for...in loop will also enumerate the inherited properties of an object. To filter out inherited properties, you can use the hasOwnProperty method:

for (const key in person) {
  if (person.hasOwnProperty(key)) {
    console.log(`${key}: ${person[key]}`);
  }
}

// Output:
// "name: John"
// "age: 30"
// "greet: function() {...}"

Object.assign() Method

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It returns the target object.

const source1 = { name: 'John' };
const source2 = { age: 30 };
const target = {};

Object.assign(target, source1, source2);
console.log(target); // Output: { name: 'John', age: 30 }

Note that the Object.assign() method only copies own properties and does not copy inherited properties. It also does not deep copy the objects; it only copies the references of nested objects.

Object.keys() and Object.values() Methods

The Object.keys() method returns an array of the object's own enumerable property names. The Object.values() method returns an array of the object's own enumerable property values.

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
};

console.log(Object.keys(person)); // Output: ["name", "age", "greet"]
console.log(Object.values(person)); // Output: ["John", 30, function() {...}]

Conclusion

Objects are an essential part of JavaScript and are used to represent real-world entities. They can have properties and methods, and can be created using the Object constructor or object literal notation. You can access and modify object properties using dot notation or bracket notation, and enumerate object properties using the for...in loop. The Object.assign() method can be used to copy the values of enumerable own properties from one or more source objects to a target object. The Object.keys() and Object.values() methods can be used to get the object's own property names and values, respectively.