Skip to content

Instantly share code, notes, and snippets.

@polotek
polotek / clone.js
Created September 24, 2011 19:22 — forked from rwaldron/clone.js
Real, deep copied objects.
function hasOwnProperty(key) {
if(this[key]) {
var proto = this.prototype;
if(proto) {
return ((key in this.prototype) && (this[key] === this.prototype[key]));
}
return true;
} else {
return false;
}
@polotek
polotek / pipeaccept.js
Created May 18, 2011 01:16 — forked from heapwolf/pipeaccept.js
accepting input from a pipe, nodejs
var data;
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
data += chunk;
});
process.stdin.on('end', function() {
var Base = {
_ops: {},
_privateFnc: function () {},
abstract: function () {},
init: function (ops) {
this._ops = ops || {};
// INIT
}
};
include("/utils.js");
// "this" scope is the object, closures work like normal. Basically this is a nowmal "call" use for functions
Object.prototype.instance_eval = function (input) {
// Convert the function to a string so that we can rebind it
if (typeof input === 'function') {
return input.call(this);
}
// Add some ruby-like methods to some of the builtins
Object.prototype.instance_eval = function (block) {
// Convert the function to a string so that we can rebind it
if (typeof block === 'function') {
block = "(" + block + ").call(this)";
}
// Eval using "this" as the "with" scope
return eval("with(this) { " + block + "}");
};
// res.write is defined by the app framework to take callbacks; it stores the
// callbacks in a queue that ensures FIFO order, and when req.end is called it
// waits for all the write callbacks to finish before ending
var hello = function(req, res) {
res.writeHead(200, {});
res.write(function(){ this('Hello World'); };
res.end();
}