Skip to content

Instantly share code, notes, and snippets.

@mrbalihai
Created November 7, 2020 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrbalihai/2ad4cd6e81794e0c0b489ead8de32b34 to your computer and use it in GitHub Desktop.
Save mrbalihai/2ad4cd6e81794e0c0b489ead8de32b34 to your computer and use it in GitHub Desktop.
Code examples for mrbalihai.github.io/2016/01/15/making-the-most-of-the-javascript-language.html
npm install -g browserify babelify
browserify -t babelify es6code.js > es5code.js
var outer = function () {
var a = 1; // variables declared outside of the 'inner' function declaration
var b = 2;
return function inner() {
return a; // return 'a' that was defined in the outer function.
// return b; // We could also access 'b' if we felt like it.
};
};
var inner = outer(); // the outer function returns the inner function
inner(); // => 1 // the inner functions still remembers that 'a' was defined as '1' from the outer function
const myArray = [1, 2, 3, 4, 5, 6]
const add = (a, b) => a + b;
const getTotal = (arr) => arr.reduce(add, 0);
getTotal(myArray); // => 21
var myArray = [1, 2, 3, 4, 5, 6]
var add = function (a, b) {
return a + b;
};
var getTotal = function (arr) {
return arr.reduce(add, 0);
};
getTotal(myArray); // => 21
var foo = function () {
var a = 2,
b = 4;
var bar = function () {
var b = 9,
c = 14;
// Here: a is 2, b is 9 and c is 14
a = a + b + c;
// Here: a is 25, b is still 9 and c is still 14
};
// Here: a is 2, b is 4 and c is undefined
bar();
// Now: a is 25, b is still 4 and c is still undefined
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment