Skip to main content

JavaScript this Keyword — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript this Keyword lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Use this in a method

    easy10 min

    Return a greeting using this.name from an object method.

  2. 2

    this is the object before the dot

    easy10 min

    Return a description using this.brand.

  3. 3

    Keep this with bind

    easy10 min

    Bind a method to its object and call it from a separate variable.

  4. 4

    An arrow keeps the outer this

    easy10 min

    Use an arrow inside a method so it can still see the object.

  5. 5

    this in a class

    medium10 min

    Create a class with a speak method and show what one instance says.

Find the bug(5)

  1. 1

    Fix the missing this

    easy8 min

    The method reads a name that does not exist in its scope.

  2. 2

    Fix the method that lost its object

    easy8 min

    Taking the method out of the object breaks the link to this.

  3. 3

    Fix bind that was never called

    easy8 min

    bind returns a function; it does not run it.

  4. 4

    Fix the normal function inside map

    easy8 min

    A normal callback has its own this, so the team name is lost.

  5. 5

    Fix the class field never set

    easy8 min

    The constructor does not store the name on the instance.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace