Loading
metasean
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
http://slides.com/metasean/cc2015-callbacks-and-closures/live#/
DevMountain / Coco@Controls
Callbacks
Closures
Closure Challenges
Functions are objects.
Pass a function,
as a parameter,
into another function.
/***********************************************************
FUNCTIONS TO CALLBACK
***********************************************************/
function add(a, b) { return a + b; }
function subtract(a, b) { return a - b; }
function multiply(a, b) { return a * b; }
function divide(a, b) { return a/b; }
function modulus(a, b) { return a % b; }
/***********************************************************
VARIABLE ASSIGNMENTS
***********************************************************/
var x = parseInt(prompt("What is X?"));
var y = parseInt(prompt("What is y?"));
/***********************************************************
OUR AWESOME CALLBACK FUNCTION
***********************************************************/
function calculate(a, b, callback) {
var result = callback(a, b);
return result;
}
/***********************************************************
OUR AWESOME CALLBACK FUNCTION
***********************************************************/
function calculate(a, b, callback) {
return callback(a, b);
}
/***********************************************************
LOGGING OUR CALLBACK FUNCTION RESULTS
***********************************************************/
console.log("For an x of " + x + " and a y of " + y + ":");
console.log("add = " + calculate(x, y, add));
console.log("subtract = " + calculate(x, y, subtract));
console.log("multiply = " + calculate(x, y, multiply));
console.log("divide = " + calculate(x, y, divide));
console.log("modulus = " + calculate(x, y, modulus));
Closures can
return a function
from a function.
(sort of)
Return a function
from a function.
function greet(msg) {
var closure = function(name) {
console.log(msg + " " + name);
};
return closure;
}
"Hello World"
function greet(msg) {
return function(name) {
console.log(msg + " " + name);
};
}
// Call the greet function, including the closure
greet("Hello")("World");
"Welcome Marvin"
function greet(msg) {
return function(name) {
console.log(msg + " " + name);
};
}
// Create a variable from the returned function
var greeting = greet("Welcome");
// Call the closure function
greeting("Marvin");
One function,
creates another function,
such that the created function
includes the creating function's lexical environment.
Alpha function,
creates Beta function,
such that Beta function
includes the Alpha function's lexical environment.
(example based definition)
function arrayOfFunctions() {
var arr = [];
for (var i = 0; i < 3; i++ ) {
arr.push(
function() { console.log(i) }
);
}
return arr;
}
var af = arrayOfFunctions();
af[0]();
af[1]();
af[2]();
function arrayOfFunctions() {
var arr = [];
for (var i = 0; i < 3; i++ ) {
arr.push(
function() { console.log(i) }
);
}
return arr;
}
var af = arrayOfFunctions();
af[0]();
af[1]();
af[2]();
// 3
// 3
// 3
Not exactly what we wanted!
}
arrayOfFunctions (aOF),
creates af,
such that af
includes aOF's lexical environment.
(example based definition)
function arrayOfFunctions() {
var arr = [];
for (var i = 0; i < 3; i++ ) {
let j = i;
arr.push(
function() { console.log(j) }
);
}
return arr;
}
var af = arrayOfFunctions();
af[0]();
af[1]();
af[2]();
// 0
// 1
// 2
What we actually wanted. :-)
}
https://www.udemy.com/understand-javascript/#/ (check http://www.retailmenot.com/view/udemy.com for regular, massive discounts off the course list price)
http://javascriptissexy.com/understand-javascript-closures-with-ease/
http://www.sitepoint.com/google-closure-how-not-to-write-javascript/
Use open data sets
Use an existing idea or come up with one of your own
2015 Competition