How to access a function property as a method in JavaScript?

In JavaScript, object properties can hold functions, which become methods when accessed through the object. A method is simply a function that belongs to an object and can access the object's other properties using the this keyword.

Syntax

const object = {
    property1: value1,
    property2: value2,
    methodName: function() {
        return this.property1 + this.property2;
    }
};

// Call the method
object.methodName();

Example 1: Employee Object with fullName Method

Here's an employee object where the fullName property contains a function that combines firstName and lastName:

<html>
<body>
<script type="text/javascript">
    var employee = {
        firstName: "raju",
        lastName: "nayak",
        designation: "Engineer",
        fullName: function() {
            return this.firstName + " " + this.lastName;
        }
    };
    document.write(employee.fullName());
</script>
</body>
</html>
raju nayak

Example 2: Student Object with Details Method

This example shows a student object with a method that returns detailed information:

<html>
<body>
<script type="text/javascript">
    var student = {
        name: "susan",
        country: "USA",
        rollNo: "5",
        details: function() {
            return "The student named " + this.name + " is allocated with rollno " + this.rollNo;
        }
    };
    document.write(student.details());
</script>
</body>
</html>
The student named susan is allocated with rollno 5

Alternative Method Syntax (ES6)

Modern JavaScript provides a shorter syntax for defining methods in objects:

<html>
<body>
<script type="text/javascript">
    var calculator = {
        x: 10,
        y: 5,
        add() {
            return this.x + this.y;
        },
        multiply() {
            return this.x * this.y;
        }
    };
    document.write("Addition: " + calculator.add() + "<br>");
    document.write("Multiplication: " + calculator.multiply());
</script>
</body>
</html>
Addition: 15
Multiplication: 50

Key Points

  • Methods are functions stored as object properties
  • Use this keyword to access other properties within the method
  • Call methods using dot notation: object.methodName()
  • ES6 provides shorthand syntax for method definitions

Conclusion

Function properties in JavaScript objects act as methods when invoked. Use the this keyword within methods to access other object properties, enabling powerful object-oriented programming patterns.

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements