Custom HTML5 Video
http://codepen.io/digilou/pen/jBgvZq
- double underscore in CSS and HTML is a sub-element that is modified
- video properties (currentTime, paused, textContent, offsetX)
- more addEventListener use!
Key Sequence Detection
http://www.cornify.com/js/cornify.js
const pressed = [];
const secretCode = 'wesbos';
window.addEventListener('keyup', (e) => {
console.log(e.key);
pressed.push(e.key);
pressed.splice(-secretCode.length - 1, pressed.length - secretCode.length);
if (pressed.join('').includes(secretCode)) {
console.log('DING DING!');
cornify_add();
}
console.log(pressed);
});
- konami code (like Easter eggs)
- cornify all the things! (unicorns and rainbows everywhere)
Slide in on Scroll
http://codepen.io/digilou/pen/vmBrXq
- debounce = limiting the rate at which events fire in order to improve performance
- copying code snippets like debounce are the way of the dev rather than reinventing the wheel
- a bit of event listening and math process involved in order for this to function
- I'm definitely leaning toward arrow functions as a preference which make methods like forEach() easier to comprehend
Object and Arrays - Reference vs Copy
- Booleans, strings, and number variable reference to a variable will not change when the original is changed
- Reference of an array references back to original array, so change happens within the original array
- To make an actual copy rather than a reference:
- array.slice()
- [].concat(array)
- [...array]
- objects work the same way in referencing as an array
- To copy an object:
- Object.assign({}, person, {number:99}) //only one level deep
- {...person} //React
// start with strings, numbers and booleans
// let age = 100;
// let age2 = age;
// console.log(age, age2);
// age = 200;
// console.log(age, age2);
// let name = 'Wes';
// let name2 = name;
// console.log(name, name2);
// name = 'wesley';
// console.log(name, name2);
// Let's say we have an array
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
// and we want to make a copy of it.
const team = players;
console.log(players, team);
// You might think we can just do something like this:
// team[3] = 'Lux';
// however what happens when we update that array?
// now here is the problem!
// oh no - we have edited the original array too!
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
// So, how do we fix this? We take a copy instead!
const team2 = players.slice();
// one day
// or create a new array and concat the old one in
const team3 = [].concat(players);
// or use the new ES6 Spread
const team4 = [...players];
team4[3] = 'heeee hawww';
console.log(team4);
const team5 = Array.from(players);
// now when we update it, the original one isn't changed
// The same thing goes for objects, let's say we have a person object
// with Objects
const person = {
name: 'Wes Bos',
age: 80
};
// and think we make a copy:
// const captain = person;
// captain.number = 99;
// how do we take a copy instead?
const cap2 = Object.assign({}, person, { number: 99, age: 12 });
console.log(cap2);
// We will hopefully soon see the object ...spread
// const cap3 = {...person};
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.
const wes = {
name: 'Wes',
age: 100,
social: {
twitter: '@wesbos',
facebook: 'wesbos.developer'
}
};
console.clear();
console.log(wes);
const dev = Object.assign({}, wes);
const dev2 = JSON.parse(JSON.stringify(wes));
Local Storage and Event Delegation
http://codepen.io/digilou/pen/PmoOYG
- a little SVG exposure
- submit event listener is best for when is button is clicked (or someone hits Enter key)
- .preventDefault() prevents page from reloading
- CSS trick for hiding checkbox to insert custom checked image
- ES6 makes innerHTML much easier to work with!
- learned about localStorage to keep list items even after refresh
- event delegation targets parent so it can relay event listeners to children