-
-
Save cfree/1033589318d6f364020f to your computer and use it in GitHub Desktop.
OOJS Revelations
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
function add() { | |
var sum = 0; | |
for (var i in arguments) { | |
sum += arguments[i]; | |
} | |
return sum; | |
} | |
add(2,4); // 6 |
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
function add(a) { | |
return function(b) { | |
return a + b; | |
} | |
} | |
var sum = add(2); | |
sum(4); // 6 | |
add(2)(4); // 6 |
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
// 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; | |
}; |
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
// 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