JSFiddle - React, Tailwind, and code Playground

by javajosh

JavaScript

function print(arg){console.log(arg);}

//A typical closure
function g(arg){return function(){print(arg++)};}
var h = g(7);
h(); //7
h(); //8


//Let's see if we can make arbitrary changes to the closure's environment
function i(arg){
    var value = 9;
    return function(){value = arg(value);};
}
function j(arg){return arg++;} 
var k = i(j);
k(); //'value is not defined'