Skip to content

Instantly share code, notes, and snippets.

View joshblack's full-sized avatar

Josh Black joshblack

View GitHub Profile
<div class="foo"></div>
<script>
document.querySelectorAll('.foo').addEventListener('click', function() {
console.log('You clicked me!');
});
// Or in jQuery
$('.foo').on('click', function() {
console.log('You clicked me!');
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<!-- ... -->
</ul>
<script>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<!-- ... -->
</ul>
<script>
function addSecretSalt() {
var secret = "JS Rules!";
return function(salt) {
return secret + salt;
}
};
// Usage
var secretAndSalt = addSecretSalt('salted'); // "JS Rules!salted"
var fs = require('fs');
fs.readFileAsync('foo.txt', function(buffer) {
var fileContents = buffer.toString();
// do stuff!
});
// Using jQuery...
$('.foo').on('click', function() {
// do stuff!
});
var MODULE = {};
MODULE.submodule = (function() {
function init() {
console.log('initiated!');
};
return {
init: init
};
// Composition
function add(x) {
return function(y) {
return x + y;
};
};
// Usage
add(1)(2); // 3
function foo() {};
foo.call(thisArg, arg1, arg2, arg3, /* ... */);
foo.apply(thisArg, [arg1, arg2, arg2, /* ... */]);
function foo() {
console.log(x); // undefined
var x = 1;
console.log(x); // 1
};
// What actually happens under the hood
function foo() {
var x;