JAVASCRIPT Tutorial
In programming, methods are functions associated with objects. They define the behavior an object can exhibit.
.
.// Create an Employee object
const employee = {
name: "John Smith",
salary: 10000,
// Define the getSalary() method
getSalary: function() {
// Use 'this' to access the object's properties
return this.salary;
}
};
// Call the getSalary() method
const salary = employee.getSalary();
console.log(`Employee ${employee.name} earns $${salary}.`); // Output: Employee John Smith earns $10000.
Object methods provide a structured way to define and execute actions on objects, enhancing their reusability and code organization. By understanding these concepts, developers can easily create and manipulate objects with specific behaviors.