Skip to content

Instantly share code, notes, and snippets.

@elarkin
elarkin / proto.js
Created December 16, 2010 04:47
.__proto__.constructor vs .constructor
var A = function A() {};
var B = function B() {};
B.prototype = new A();
// B.prototype.constructor = B; // only way I can find to get bi's constructor to be B. Comment or uncomment this line to produce exactly the same output for both tests. It doesn't matter.
// require('util').inherits(B, A); // same here. Comment or uncomment it doesn't make a difference.
var ai = new A();
var bi = new B();
console.log('Test 1: constructor directly');
@elarkin
elarkin / paramtest.js
Created November 28, 2010 18:24
Proves that eventemitter.emit works as expected.
var sys = require('sys')
var EventEmitter = require('events').EventEmitter
var func = function(arg1, arg2) {
if(arg1 === undefined && arg2 === undefined) {
sys.puts("I was called without parameters");
}
};
var emitter = new EventEmitter();
emitter.on('test',func)
@elarkin
elarkin / Mutex.js
Created November 24, 2010 03:53
A simple mutex library for node.js v0.3.1
var EventEmitter = require('events').EventEmitter
var Mutex = function() {
var queue = new EventEmitter();
var locked = false;
this.lock = function lock(fn) {
if (locked) {
queue.once('ready',function() {
lock(fn);
});
} else {