Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created October 31, 2012 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/259caead871bcc42d8bc to your computer and use it in GitHub Desktop.
Save bnoordhuis/259caead871bcc42d8bc to your computer and use it in GitHub Desktop.
call JS object as a function
function Register(id, index)
{
if (this instanceof Register) {
this.index = index;
this.id = id;
}
else {
var self = new Register(id, index);
var reg = self.funcall.bind(self);
reg.__proto__ = self;
reg.index = index;
reg.id = id;
return reg;
}
}
Register.prototype = new Function;
Register.prototype.funcall = function(reg, disp) {
return this;
};
var assert = require('assert');
var eax = Register('eax', 0);
assert(eax instanceof Register);
assert(eax instanceof Function);
assert(eax() !== eax); // === self, not reg :(
assert(eax.id === 'eax');
assert(eax.name === '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment