In this practice, you will practice identifying when to use bind
and how to
use it.
Clone the starter from the Download link at the bottom of this page.
Implement the following in the employee.js file.
-
Create a class called
Employee
. -
The constructor method should take in a
name
andoccupation
for theEmployee
and initialize them as instance variables. -
Add an instance method called
sayName()
that willconsole.log
theEmployee
's name like so:${name} says hello
. -
Add another instance method called
sayOccupation()
that willconsole.log
theEmployee
's name and occupation like so:${name} is a ${occupation}
. -
Export the
Employee
class from the file.
To test your code, implement the following in the test.js file.
-
Import the
Employee
class. -
Create a new
Employee
with thename
of "John Wick" and anoccupation
of "Dog Lover". -
Call the
sayName()
instance method on the newEmployee
in asetTimeout
after 2 seconds. Question: What do you expect to be printed to the terminal and why?Solve the problem using `bind` to make sure that "John Wick says hello" will be printed to the terminal after 2 seconds.Answer:
"undefined says hello" will be printed to the terminal because the `sayName()` instance method on the new `Employee` was invoked function-style by the `setTimeout` after 2 seconds. -
Call the
sayOccupation()
instance method on the newEmployee
in asetTimeout
after 3 seconds. Question: What do you expect to be printed to the terminal and why?Solve the problem using `bind` to make sure that "John Wick is a Dog Lover" will be printed to the terminal after 3 seconds.Answer:
"undefined is a undefined" will be printed to the terminal because the `sayOccupation()` instance method on the new `Employee` was invoked function-style by the `setTimeout` after 3 seconds.