Exercise 12.5
This is Exercise 12.5, lecture on advanced functions in Javascript, used in the course IDATA2301 Web technologies at NTNU, campus Aalesund.
Purpose
The purpose of the exercise is practice setting the
this
"variable" using .apply
.
Instructions
Create a new HTML document, and include a Javascript file called
app.js
. Use that file to create the following
functionality.
-
Create an object
person
, which has two properties:firstname
andlastname
. -
Add a function named
printName
to that object. It should print the person's name to the console. -
Create a new object called
student
, add the same two properties for it:firstname
andlastname
. Set your values for it (your firstname or lastname, unless you are Batman - then use "Bat" as firstname, "Man" (or "Woman") as lastname). Note: thestudent
object must not have theperson
as the prototype, otherwise this exercise won't make sense. -
Now borrow the
person.printName()
method and call it on thestudent
object. Hint: you can use the.apply()
theprintName
function to thestudent
object. Run it to see the output in the console.
Solution
You can find a solution here.