JavaScript objects are collections of properties, and methods are properties that hold function definitions. Object methods provide a way to encapsulate functionality within an object, allowing you to perform actions related to that object. This guide will cover the basics of defining, accessing, and using JavaScript object methods.

1. Defining Object Methods:

Object methods can be defined in several ways, including using function expressions, function declarations, and ES6 method syntax.

1.1 Using Function Expressions:

You can define methods using function expressions within an object literal.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.fullName()); // Outputs: John Doe
1.2 Using Function Declarations:

Although less common, you can also use function declarations to define methods. However, this typically involves defining the function outside the object and then assigning it as a property.

Example:

function getFullName() {
  return this.firstName + " " + this.lastName;
}

let person = {
  firstName: "John",
  lastName: "Doe",
  fullName: getFullName
};

console.log(person.fullName()); // Outputs: John Doe
1.3 Using ES6 Method Syntax:

ES6 introduced a shorthand syntax for defining methods within object literals.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  fullName() {
    return this.firstName + " " + this.lastName;
  }
};

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

2. Accessing Object Methods:

Object methods are accessed and called similarly to object properties, using dot notation or bracket notation.

2.1 Dot Notation:

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  fullName() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.fullName()); // Outputs: John Doe
2.2 Bracket Notation:

Bracket notation can be useful when the method name is stored in a variable or contains characters that are not valid in an identifier.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  fullName() {
    return this.firstName + " " + this.lastName;
  }
};

let methodName = "fullName";
console.log(person[methodName]()); // Outputs: John Doe

3. Using this Keyword:

Within a method, this refers to the object on which the method was called. This allows methods to access and modify the object’s properties.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  setFirstName(newFirstName) {
    this.firstName = newFirstName;
  },
  fullName() {
    return this.firstName + " " + this.lastName;
  }
};

person.setFirstName("Jane");
console.log(person.fullName()); // Outputs: Jane Doe

4. Adding Methods to Existing Objects:

You can add methods to an existing object using dot notation or Object.defineProperty().

4.1 Using Dot Notation:

Example:

let person = {
  firstName: "John",
  lastName: "Doe"
};

person.fullName = function() {
  return this.firstName + " " + this.lastName;
};

console.log(person.fullName()); // Outputs: John Doe
4.2 Using Object.defineProperty():

Object.defineProperty() allows you to define a method with specific property attributes.

Example:

let person = {
  firstName: "John",
  lastName: "Doe"
};

Object.defineProperty(person, 'fullName', {
  value: function() {
    return this.firstName + " " + this.lastName;
  },
  writable: true,
  enumerable: true,
  configurable: true
});

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

5. Common Built-in Object Methods:

JavaScript provides several built-in methods for working with objects, such as Object.keys(), Object.values(), Object.entries(), Object.assign(), and Object.freeze().

5.1 Object.keys():

Returns an array of the object’s own property names.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

console.log(Object.keys(person)); // Outputs: ["firstName", "lastName", "age"]
5.2 Object.values():

Returns an array of the object’s own property values.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

console.log(Object.values(person)); // Outputs: ["John", "Doe", 30]
5.3 Object.entries():

Returns an array of the object’s own [key, value] pairs.

Example:

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

console.log(Object.entries(person)); // Outputs: [["firstName", "John"], ["lastName", "Doe"], ["age", 30]]
5.4 Object.assign():

Copies the values of all enumerable properties from one or more source objects to a target object.

Example:

let target = { a: 1 };
let source = { b: 2, c: 3 };

Object.assign(target, source);
console.log(target); // Outputs: { a: 1, b: 2, c: 3 }
5.5 Object.freeze():

Freezes an object, preventing new properties from being added and existing properties from being removed or modified.

Example:

let person = {
  firstName: "John",
  lastName: "Doe"
};

Object.freeze(person);
person.age = 30; // This will not work
console.log(person.age); // Outputs: undefined

Conclusion:

JavaScript object methods are essential for encapsulating functionality and interacting with object properties. By understanding how to define, access, and use methods, you can create more organized and maintainable code. Utilizing built-in object methods further enhances your ability to manage and manipulate objects effectively in JavaScript.