Skip to content

Instantly share code, notes, and snippets.

@djsmith42
Last active August 29, 2015 14:02
Show Gist options
  • Save djsmith42/031da4151807ccb25d7e to your computer and use it in GitHub Desktop.
Save djsmith42/031da4151807ccb25d7e to your computer and use it in GitHub Desktop.
function counter() {
var n = 0;
return function() {
n++;
return n;
}
}
c = counter();
c(); // ouputs 1
c(); // outputs 2
$("button").click(function(event) {
// handle the click
}
var handlers = [
function() { ... },
function() { ... }
];
var thingsToRun = {
red: function() { ... },
blue: function() { ... },
green : function() { ... }
}
(function () {
var name = "Bob, the Closure String";
$("button").click(function() {
alert("My closure has a first name. It's " + name)
});
})();
function capitalize(list) {
for (var i=0; i<list.length; i++) {
list[i] = list[i].toUpperCase();
}
}
function upper(str) {
return str.toUpperCase();
}
function capitalize(list) {
return list.map(upper)
}
lowerCase(underscore(capitalize(['foo', 'bar'])))
$.get("http://example.com")
.then(doSomething)
.then(doSomethingElse)
.then(andFinish)
function findNextPrime(start) {
// do lots of work
return nextPrime;
}
function findNextPrime(start) {
return function() {
// do lots of work
return nextPrime;
}
}
translated_string = trans("Hello, world!")
function Thing() {
this.name = "Bob";
this.age = 42;
}
var thing = new Thing();
var x = {name: "Bob"};
x.name = "Fred"; // Not allowed!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment