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:firstnameandlastname. -
Add a function named
printNameto 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:firstnameandlastname. Set your values for it (your firstname or lastname, unless you are Batman - then use "Bat" as firstname, "Man" (or "Woman") as lastname). Note: thestudentobject must not have thepersonas the prototype, otherwise this exercise won't make sense. -
Now borrow the
person.printName()method and call it on thestudentobject. Hint: you can use the.apply()theprintNamefunction to thestudentobject. Run it to see the output in the console.
Solution
You can find a solution here.