JavaScript Object and it's method

JavaScript Object and it's method

·

3 min read

Introduction- JavaScript is a versatile and powerful programming language that allows developers to create dynamic and interactive web applications. One of the key features of JavaScript is its ability to work with objects. Objects are a fundamental building block of JavaScript, providing a way to organize and manipulate data. we will delve into JavaScript objects and explore their methods, unlocking the potential for creating efficient and robust code.

javascriptCopy code// Creating an object using object literal syntax
const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,

  // Method to get the full name
  getFullName: function() {
    return this.firstName + ' ' + this.lastName;
  },

  // Method to greet the person
  greet: function() {
    console.log('Hello, ' + this.getFullName() + '!');
  }
};

console.log(person.firstName); // Output: John
console.log(person.age); // Output: 30
console.log(person.getFullName()); // Output: John Doe
person.greet(); // Output: Hello, John Doe!

we create an object called person with properties firstName, lastName, and age. We also define two methods: getFullName and greet. The getFullName method concatenates the firstName and lastName properties and returns the full name. The greet method calls the getFullName method and logs a greeting message to the console.

To access the properties and methods of an object, we use dot notation (objectName.propertyName or objectName.methodName()). The this keyword inside a method refers to the current object, allowing us to access its properties and invoke other methods.

Note that there are other ways to create objects in JavaScript, such as using the new keyword and constructor functions, but the object literal syntax shown above is the most common and straightforward approach.

JavaScript Object method

In JavaScript, object methods are functions that are defined as properties of an object. These methods can be used to perform actions or provide functionality associated with the object. Here's a description of JavaScript object methods along with an example:

  1. Defining an Object Method: To define an object method, you assign a function expression or function declaration to a property of an object. The method can then be invoked using the object name followed by the property name and parentheses.
javascriptCopy codeconst myObject = {
  myMethod: function() {
    // Method implementation
  }
};

myObject.myMethod(); // Invoking the object method
  1. Accessing Object Properties: Inside an object method, you can access other properties of the object using the this keyword. It refers to the current object and allows you to access its properties and methods.
javascriptCopy codeconst person = {
  firstName: 'John',
  lastName: 'Doe',
  getFullName: function() {
    return this.firstName + ' ' + this.lastName;
  }
};

console.log(person.getFullName()); // Output: John Doe
  1. Using Parameters: Object methods can accept parameters, allowing passing values to the method and use them within the method's implementation.
javascriptCopy codeconst calculator = {
  add: function(a, b) {
    return a + b;
  }
};

console.log(calculator.add(5, 3)); // Output: 8
  1. Using Method Shorthand: In modern JavaScript, you can use the shorthand method syntax, which allows you to omit the function keyword when defining object methods.
javascriptCopy codeconst myObject = {
  myMethod() {
    // Method implementation
  }
};

myObject.myMethod(); // Invoking the object method
  1. Using Arrow Functions: we can also use arrow functions as object methods. Arrow functions have a lexical this binding, which means they inherit the this value from the surrounding scope.
javascriptCopy codeconst myObject = {
  myMethod: () => {
    // Method implementation
  }
};

myObject.myMethod(); // Invoking the object method

Object methods are a powerful feature in JavaScript as they allow objects to encapsulate behavior and provide a convenient way to organize and manipulate data associated with the object.