$elem.on("click", handler);
$elem.on("keydown", handler);
function handler(event) {
if (event.type === "keydown") {
// ...
} else if (event.type === "click") {
// ...
}
// common functionality
}
Closures
This was fun! CodeSchool did a great job in Javascript Road Trip Part 3, but it just didn’t click for my son right away. Closures still trip me up to this day.
My first explanation attempt:
“A closure is like a dinosaur fossil — a snapshot from a moment in time preserved for millions of years. You can get still get information about the dinosaur from the fossil, even though the dinosaur itself has been gone for millions of years.”
Closures were starting to make sense, so I tried using cookies again . . .
“Let’s say I go to the same bakery every day and ask the baker for a cookie. The first thing he/she asks is, ‘What kind of cookie do you want?’ After a few days, the baker might already know what cookie I like and simply ask, ‘The usual?’. By saving my cookie choice in memory, he/she is able to reuse the same function from the previous day to get me the proper cookie without having to ask again.”
Eh… It was still a bit fuzzy, until my son asked me…
“So, it’s like when we go to the barber and he just cuts our hair without asking what we want? He always asks new customers how they want him to cut, but never asks us anymore.”
Ding, ding, ding! He got it!!!
// ...
gulp.task('serve', ['...', 'styles', '...'], function () {
browserSync.init({
server: {
baseDir: './dist/'
},
browser: 'google chrome',
notify: false
});
gulp.watch('src/styles/**/*.styl', ['styles']);
});
// ...
gulp.task('styles', function () {
gulp.src('src/styles/main.styl')
.pipe(plumber())
.pipe(stylus())
.pipe(autoprefixer({ browsers: ['last 3 version'] }))
.pipe(browserSync.stream())
.pipe(gulp.dest('dist/styles'));
});
// ...