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.

Go back to exercise list.

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.

  1. Create an object person, which has two properties: firstname and lastname.
  2. Add a function named printName to that object. It should print the person's name to the console.
  3. Create a new object called student, add the same two properties for it: firstname and lastname. Set your values for it (your firstname or lastname, unless you are Batman - then use "Bat" as firstname, "Man" (or "Woman") as lastname). Note: the student object must not have the person as the prototype, otherwise this exercise won't make sense.
  4. Now borrow the person.printName() method and call it on the student object. Hint: you can use the .apply() the printName function to the student object. Run it to see the output in the console.

Solution

You can find a solution here.