Giter Club home page Giter Club logo

closure_demo's Introduction

What is Closure?

Closure describes a behavior of JavaScript that does not exist in Ruby. In both languages, variables have a scope in which they can be used, with some differences.

Test Files

Run these test files in your terminal. Note their behavior then read the Ruby and JavaScript sections below.
ruby rb_test.rb
node js_test.js

Ruby

First let's show how Ruby functions do not have closure; they do not have access to variables unless they are passed in as arguments or defined inside the function.

def outer_function(arg)

    def inner_function
        print(arg) #'arg' is not passed into the inner function
        #Raise error because 'arg' is not defined in the inner function
    end

    inner_function()
end

#Invoke the outer_function, passing in a string:
outer_function("THIS IS CLOSURE.") #=> NameError undefined variable 'arg'

This code raises an error and does not print THIS IS CLOSURE. Can you think of a way to fix this ruby code?

JavaScript

Now, let's see how JavaScript handles this:

function outer_function(arg){

    function inner_function(){ //'arg' is not passed into the inner function
        console.log(arg) 
        //We can still print arg within the inner function's scope.
    }

    inner_function();
}

//Invoke the outer_function, passing in a string:
outer_function("This is Closure!"); //=> This is Closure!

This code does not throw an error. Why? Because arg was passed into outer_function as an argument. This means that any functions defined within outer_function will also have access to arg.
This would also apply to any variable defined in the outer_function, not just arguments that were passed in.

closure_demo's People

Contributors

siegetheday90 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.