Skip to content

Instantly share code, notes, and snippets.

View yangsu's full-sized avatar

Yang Su yangsu

  • San Francisco, CA
  • X @ys65
View GitHub Profile
@yangsu
yangsu / javascript-aug-types.js
Created January 29, 2013 21:21
JavaScript Augmenting Types
Number.prototype.times = function (fn, params, context) {
var i = this;
while (i > 0) {
if (params || context) {
fn.apply(context || i, params);
} else {
fn.apply(context || i);
}
i--;
}
@yangsu
yangsu / javascript-closure.js
Created January 29, 2013 21:24
JavaScript Closure Examples
function counter (init) {
// each time counter is called, i is local to each closure
var i = init || 0;
var incrementer = function (inc) {
inc = inc || 1;
i += inc;
return i;
}
return incrementer;
}
@yangsu
yangsu / javascript-closure-error.js
Last active December 11, 2015 22:09
JavaScript Closure Error Exampel
var content = '<h1><p>1</p></h1><h2><p>2</p></h2><h3><p>3</p></h3>';
var html = '<html><body>' + content + '</body></html>';
document.write(html);
var badHandlers = function (nodes) {
var i;
for (i = 0; i < nodes.length; i++) {
nodes[i].onclick = function (e) {
alert(i); // always returns nodes.length?!
}
}
@yangsu
yangsu / javascript-callbacks.js
Created January 29, 2013 21:26
JavaScript Callbacks
// Synchronously
var req = preparePayload();
var res = sendRequestSync(req); // This will block!
displayResponse(res);
// Asynchronously
var req = preparePayload();
sendRequestAsync(req, displayResponse);
function preparePayload() { // ... }
@yangsu
yangsu / javascript-return.js
Created January 29, 2013 21:31
JavaScript Return Value
function a() { return 5; }
function b() { return; }
function c() { }
function D() { this.value = 5; }
console.log(a()); // 5
console.log(b()); // undefined
console.log(c()); // undefined
console.log(new D().value); // 5
@yangsu
yangsu / javascript-pseudoclassical.js
Last active December 11, 2015 22:18
JavaScript Pseudoclassical Example
var Mammal = function (name) {
this.name = name;
};
Mammal.prototype.get_name = function () {
return this.name;
};
Mammal.prototype.says = function () {
return this.saying || '';
};
Mammal.classifier = function () {
@yangsu
yangsu / javascript-prototypal.js
Created January 29, 2013 23:01
JavaScript Prototypal Inheritance
Object.beget = function (o) {
var F = function () {};
F.prototype = o;
return new F();
};
var myMammal = {
name: 'Herb the Mammal',
get_name: function () {
return this.name;
@yangsu
yangsu / javascript-function.js
Last active December 11, 2015 22:18
JavaScript Functional Inheritance
var mammal = function (spec) {
var that = {};
var private = "secret stuff";
var private_method = function () {};
that.get_name = function () {
return spec.name;
};
that.says = function () {
return spec.saying || '';
};
@yangsu
yangsu / ruby-selection-sort.rb
Created January 31, 2013 22:59
Ruby Selection Sort Example
def sort(arr)
arr.each_with_index {|value, index|
rest = arr[index...arr.size]
min = rest.min
minIndex = rest.index(min) + index
if not minIndex.nil?
arr[index] = min
arr[minIndex] = value
end
@yangsu
yangsu / javascript-async-parallel.js
Created February 5, 2013 00:00
Javascript Simplified Async.js Parallel Calls
/**
* @param hash is an hash of names to functions
* @param callback is the final function
*/
function parallel(hash, callback) {
var i = 0;
var count = 0;
var retval = {};
var cb = function(err, key, data) {
if (err) {