Last active
December 12, 2016 17:12
-
-
Save hemepositive/1e616580b3deb3a0cd004525de8f81d1 to your computer and use it in GitHub Desktop.
ES6 Notes
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
// forEach | |
var numbers = [1,2,3,4,5]; | |
var sum = 0; | |
// anonymous inner function | |
numbers.forEach(function(number) { | |
sum += number; | |
}); | |
// named inner function | |
function adder(number) { | |
sum += number; | |
}; | |
numbers.forEach(adder); // inner function does not use parens or have to be passed parameter!!! | |
// map | |
incrementByOne = function (element) { | |
return element + 1; | |
} | |
myArray = [1,2,3,4]; | |
myArray.map(incrementByOne); // returns [2,3,4,5] | |
//reduce | |
// syntax | |
array.reduce(function(parameterOne, parameter2){ my iterator function }, initial value); | |
// parameter1 is initial value getting passed through, parameter2 is array element | |
var numbers = [10, 20, 30]; | |
var sum = 0; | |
numbers.reduce(function(sum, numbers){ | |
return sum + numbers; | |
}, 0); | |
var primaryColors = [ | |
{color: 'red'}, | |
{color: 'yellow'}, | |
{color: 'blue'} | |
]; | |
// acc or accumulator are good parameter names for the initial value passed to reduce... | |
primaryColors.reduce(function(previous, color){ | |
previous.push(color.color); | |
return previous | |
}, []); | |
//the balance parens problem | |
// given a string of some number of parentheses determine if the parens are balanced. | |
function balancedParens(string) { | |
// convert string to array and return bool for the reduce | |
return !string.split("").reduce(function(previous, char){ | |
if previous < 0 { return previous } // if starts with ) | |
if (char === "(") { return ++previous; } | |
if (char === ")"){ return --previous; } | |
else { return previous} | |
}, 0); | |
} | |
balancedParens("()()"); | |
// FAT ARROW | |
// non fat arrow has a problem with this | |
const team = { | |
members: ["Dick", "Jane"], | |
teamName: "Super Squad", | |
teamSummary: function(){ | |
var self = this; | |
return this.members.map(function(member) { | |
return `${member} is a member of ${this.teamName}!` | |
}); | |
} | |
}; | |
team.teamSummary(); | |
// FAT ARROW solves this | |
const team = { | |
members: ["Dick", "Jane"], | |
teamName: "Super Squad", | |
teamSummary: function(){ | |
return this.members.map((member) => | |
return `${member} is a member of ${this.teamName}!` | |
); | |
} | |
}; | |
team.teamSummary(); | |
// destructuring | |
const ray = [[1,2],[3,4],[4,5]]; | |
// some ES6 | |
const a = ray.map(pair => { | |
const [ x,y ] = pair; | |
return {x: x, y: y}; | |
}); | |
// full ES6 | |
const b = ray.map(([x,y]) =>{ | |
return { x, y } | |
}); | |
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
Good JS stuff: http://www.datchley.name/basic-scope/ (good series on JS) | |
conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement >> condition ? expr1 : expr2 | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator | |
// "The fee is " + (isMember ? "$2.00" : "$10.00") | |
ARRAY HELPERS | |
A better way to do for loops; interate over an array | |
built-in array helper methods => forEach map filter find every some reduce | |
examples: | |
http://www.datchley.name/working-with-collections/ | |
forEach | |
interates over elements in an array which are then passed to an inner function | |
inner function can be anonymous or named | |
map | |
"Looking at it this way, we can start to see what’s going on. map is a function that: | |
1 -Takes an array and a function | |
2 - Applies the function to every element in the array | |
3 - Keeps track of the results of each successive function call | |
4 - Returns a new array containing these results | |
The function taken as argument by map is known as the callback, because it’s called on every element in the original array. | |
And you have to write it in a very specific manner. The callback has to: | |
1.Take in the current element being processed as argument. | |
2. Return the new element that’s going to take its place in the new array." - from discovermeteor.com | |
map vs forEach ==> map returns an new array; forEach doesn't return anything | |
every & some >> return a boolean if *every* element or *some* elements in array after every element passed to function | |
ie does every element whatever AND does any element whatever | |
reduce | |
FAT ARROW => makes this behave like is desirable and expected; "binds" to the current context like inside an object. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment