Skip to content

Instantly share code, notes, and snippets.

View cameronism's full-sized avatar

Cameron Jordan cameronism

View GitHub Profile
// Exercise 2 - Closures
// Wrap the following code in a closure and export only the "countdown" function.
// Code
(function(container, name){
var index;
function log(){
console.log(index);
// Exercise 1 - OO || !OO
// Define a data structure for cars (make and color), and a function
// that logs a string like "I'm a red Mercedes" to the console.
// Make two versions: a functional version, and a object-oriented version.
function logCar (c) {
console.log("I'm a " + c.color + " " + c.make);
}
function Person (name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
}
// Part 1.
// Implement a function prototype extension that caches function results for
// the same input arguments of a function with one parameter.
Function.prototype.cached = function() {
var cache = {},
func = this;
return function( arg ) {
return cache[ arg ] = arg in cache ? cache[ arg ] : func(arg);
// 1
document.getElementById('the_div').addEventListener(
'click', function(){ log('the_div!') }, true);
document.getElementById('the_list').addEventListener(
'click', function(){ log('the_list!') }, !true);
document.getElementById('the_item').addEventListener(
'click', function(){ log('the_item!') }, true);
@cameronism
cameronism / Array.insertAt.js
Created March 24, 2011 19:09
Add one or more elements at the specified index and returns the new length of the array.
// Syntax
// ------
// array.insertAt(index, element1, ..., elementN)
// Parameters
// ------
// index
// : Index at which to insert elements. If negative, will begin that many elements from the end.
// element1, ..., elementN
// : The elements to add to the array.
@cameronism
cameronism / xhr.js
Created January 19, 2012 20:27
XHR and JSON is all you really need
(function(context) {
var xhr = context.XMLHttpRequest,
json = context.JSON,
mime = 'application/json';
context.xhr = xhr && json && function(method, url, data, callback) {
var req = new xhr();
req.onreadystatechange = function() {
if(req.readyState == 4) {
@cameronism
cameronism / updateClasses.js
Created September 14, 2012 00:53
Set classes on an element from an object
function updateClasses(el, options) {
var classes = el.className.split(/\s+/),
className,
pattern = /^no-(\w+)$/,
val,
i, l;
for (i = 0, l = classes.length; i < l; i++) {
className = classes[i];
if (!options.hasOwnProperty(className)) {
@cameronism
cameronism / LICENSE.txt
Created December 18, 2012 20:19 — forked from 140bytes/LICENSE.txt
Clearly the internet does not have enough kittens
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@cameronism
cameronism / mergeSorted.js
Created March 12, 2013 07:34
Creates a new sorted array with all elements from independently sorted array parameters.
// returns a new sorted array with all elements from independently sorted arrays in arguments[1] thru arguments[n]
function mergeSorted(iterator/* ... arrays */) {
var result = [],
arrays = [].slice.call(arguments, 1),
indexes = [],
values = [],
len,
ix,
min;