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
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--; | |
} |
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 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; | |
} |
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
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?! | |
} | |
} |
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
// Synchronously | |
var req = preparePayload(); | |
var res = sendRequestSync(req); // This will block! | |
displayResponse(res); | |
// Asynchronously | |
var req = preparePayload(); | |
sendRequestAsync(req, displayResponse); | |
function preparePayload() { // ... } |
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 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 |
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
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 () { |
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
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; |
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
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 || ''; | |
}; |
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
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 |
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
/** | |
* @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) { |