JSFiddle - React, Tailwind, and code Playground
by migerh
JavaScript
var a = 4,
// x squared
// This is not a closure. It is just a function expression. The context of this function is not exported
// to the outer context, hence the JavaScript engine can free it after the function was executed.
f = function (x) {
var i = 3;
return i * a * x * x;
},
// 20
// This is an iife (immediately invoked function expression), this is an anonymous function expression which
// is executed (invoked) right after the definition (note the two braces after the } closing the function body).
// Just like f, this function does not export anything to the outer context, hence, not a closure.
iife = (function () {
var i = 5;
return i * 4;
}()),
// closure
cl = function () {
var i,
len = 10,
samples = [],
f = function (x) {
return x * x;
};
// generate a table of sample values
for (i = 0
// imagine this being a very simple kind of "curve" object...
return {
// ... that holds its definition in TeX format, not important
expression: 'f: \mathbb{R} \to \mathbb{R}, x \mapsto x^2',
};
};