GithubHelp home page GithubHelp logo

closures's Introduction

Closures

Closure exercises

  1. Write a function, nonsense that takes an input string. This function contains another function, blab which alerts string and is immediately called inside the function nonsense. blab should look like this inside of the nonsense function:

     var blab = function(){
       alert(string);
     };
  2. In your function, nonsense, change the immediate call to a setTimeout so that the call to blab comes after 2 seconds. The blab function itself should stay the same as before.

  3. Now, instead of calling blab inside of nonsense, return blab (without invoking it). Call nonsense with some string and store the returned value (the blab function) in a variable called blabLater. Call nonsense again with a different string and store the returned value in a variable called blabAgainLater.

  4. Inspect blabLater and blabAgainLater in your console. Call them (they are functions!) and see what happens!

  5. Write a function with a closure. The first function should only take one argument, someone's first name, and the inner function should take one more argument, someone's last name. The inner function should console.log both the first name and the last name.

    var lastNameTrier = function(firstName){
       //does stuff
    
        var innerFunction = function() {
            //does stuff
        };
        //maybe returns something here
    };
    var firstNameFarmer = lastNameTrier('Farmer'); //logs nothing
    firstNameFarmer('Brown'); //logs 'Farmer Brown'

    This function is useful in case you want to try on different last names. For example, I could use firstName again with another last name:

    firstNameFarmer('Jane'); //logs 'Farmer Jane'
    firstNameFarmer('Lynne'); //logs 'Farmer Lynne'
  6. Create a storyWriter function that returns an object with two methods. One method, addWords adds a word to your story and returns the story while the other one, erase, resets the story back to an empty string. Here is an implementation:

    var farmLoveStory = storyWriter();
    farmLoveStory.addWords('There was once a lonely cow.'); // 'There was once a lonely cow.'
    farmLoveStory.addWords('It saw a friendly face.'); //'There was once a lonely cow. It saw a friendly face.'
    
    var storyOfMyLife = storyWriter();
    storyOfMyLife.addWords('My code broke.'); // 'My code broke.'
    storyOfMyLife.addWords('I ate some ice cream.'); //'My code broke. I ate some ice cream.'
    storyOfMyLife.erase(); // ''

closures's People

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.