Giter Club home page Giter Club logo

arrow-function-context's Introduction

Practice: Context with Arrow functions

In this practice, you will explore arrow functions and their unique ability to retain the context of where it was defined.

Set up

Clone the starter from the Download link at the bottom of this page.

Instructions

You will be debugging a context bug by using a fat arrow function in the class definition.

Take a look at the code in the index.js file.

The code instantiates a new User which is imported from the classes/user.js file. An instance of a User has a property of name and a method of changeName(newName) that will set the name property to the newName passed in as an argument.

Back in the index.js file, a function called greetAfterNameChange(changeName, newName) is defined. This function accepts a changeName function and invokes it with the newName passed in. The return value of the changeName function is saved to a variable called name and is printed to the console like so: Hi my name is ${name}.

If you run the index.js file with node index.js, the following should be printed to the terminal:

Hi my name is Elle

However, there is a problem with the code. When invoking greeAfterNamechange(changeName, newName) with the changeName instance method on the newly instantiated user, the context of the changeName function is lost so there will be an error thrown when running the index.js file.

Use your debugging skills to change the code to use an arrow function to solve this context problem.

Hint: Use an arrow function somewhere in the User class definition to make sure that the changeName instance method retains its context.

arrow-function-context's People

Contributors

500ej avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.