As mentioned earlier, objects can have methods that we can invoke to perform some kind of function or action on the object
Methods are stored as an object property, but are programmed by defining a new function
To look at a few examples, we will use an object representing an employee of an imaginary company named Katie Johnson.
This object stores the information for employee Katie Johnson, with properties for her name, ID, phone number, address, hourly wage, and number of hours worked this week.
The last property of the employee object will be the method called “printEmployeeInfo”
The printEmployeeInfo method declares a new function that returns the employee ID and full name in a formatted order
You’ll notice that the method uses a new keyword we haven’t worked with yet, “this”
When used in an object method, the ‘this’ keyword refers to the object in context.
For example within our printEmployeeInfo method, this.firstName is equivalent to saying employee.firstName
let employee = {
firstName: "Katie",
lastName: "Johnson",
employeeID: 295025,
phone: 4560001234,
address: "42 Wallaby Way",
wage: 13.5,
hoursWorked: 25,
printEmployeeInfo: function() {
return "Employee #" + this.employeeID + " : " + this.firstName + " " + this.lastName;
}
};
console.log(employee.printEmployeeInfo());
Console view:
In codepen, we will add a method to to the employee object that calculates their weekly pay This method will be called “paycheck” and will return the product of the employee object’s wage property and the employee object’s hoursWorked property.
Codepen example:
See the Pen Untitled by LSU DDEM (@lsuddem) on CodePen.
See the Pen Exercise 4.7 by LSU DDEM (@lsuddem) on CodePen.
let employee = {
firstName: "Katie",
lastName: "Johnson",
employeeID: 295025,
phone: 4560001234,
address: "42 Wallaby Way",
wage: 13.50,
hoursWorked: 25,
printEmployeeInfo: function () {
return "Employee #" + this.employeeID + " : " + this.firstName + " " + this.lastName;
}
};
console.log(employee.printEmployeeInfo());
employee.paycheck = function () {
return "$" + (this.wage * this.hoursWorked);
}
let income = employee.paycheck();
console.log(income);
console.log("Employee #" + employee.employeeID + " worked "
+ employee.hoursWorked + " hours this week and earned "
+ employee.paycheck());
employee.hoursWorked = 40;
console.log("Employee #" + employee.employeeID + " worked " +
employee.hoursWorked + " hours this week and earned " +
employee.paycheck());
employee.contact = function () {
return "Employee #" + this.employeeID + " - " + this.lastName +
", " + this.firstName + " , Phone: (" +
this.phone.toString().slice(0, 3) + ")-" + this.phone.toString().slice(3, 6) +
"-" + this.phone.toString().slice(6, 10) + " , Address: " + this.address;
}
console.log(employee.contact());