Created
November 7, 2020 19:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install -g browserify babelify | |
browserify -t babelify es6code.js > es5code.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myArray = [1, 2, 3, 4, 5, 6] | |
const add = (a, b) => a + b; | |
const getTotal = (arr) => arr.reduce(add, 0); | |
getTotal(myArray); // => 21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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