Skip to content

Instantly share code, notes, and snippets.

@cfree
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfree/1033589318d6f364020f to your computer and use it in GitHub Desktop.
Save cfree/1033589318d6f364020f to your computer and use it in GitHub Desktop.
OOJS Revelations
function add() {
var sum = 0;
for (var i in arguments) {
sum += arguments[i];
}
return sum;
}
add(2,4); // 6
function add(a) {
return function(b) {
return a + b;
}
}
var sum = add(2);
sum(4); // 6
add(2)(4); // 6
// The JavaScript engine hoists the function declaration to the top
fullName('Craig', 'Freeman'); // "Freeman, Craig"
function fullName(firstName, lastName) {
return lastName + ', ' + firstName;
}
// The JavaScript engine only recognizes the function expression
// as a variable
fullName('Craig', 'Freeman'); // Uncaught TypeError: undefined is not a function
var fullName = function(firstName, lastName) {
return lastName + ', ' + firstName;
};
// Assuming there's an unknown amount of custom global Map objects
// window.Map1, window.Map3, window.Map5, etc.
var mapIds = [1, 3, 5, 7, 9],
maps = [];
for (var i in mapIds) {
maps[] = window.['Map' + i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment