-
-
Save bnoordhuis/259caead871bcc42d8bc to your computer and use it in GitHub Desktop.
call JS object as a 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
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