Created
February 2, 2013 20:07
-
-
Save puffnfresh/4699039 to your computer and use it in GitHub Desktop.
Automatic creation of constructors.
This file contains hidden or 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
// TODO: Automatically create a useful isEqual function. | |
function valueObject() { | |
var names = arguments; | |
function wrapped() { | |
var o = this instanceof wrapped ? this : {}, i; | |
if(arguments.length != names.length) { | |
throw new TypeError("Expected " + names.length + " arguments, got " + arguments.length); | |
} | |
for(i = 0; i < names.length; i++) { | |
o[names[i]] = arguments[i]; | |
} | |
return o; | |
} | |
return wrapped; | |
} | |
var Let = valueObject('name', 'params', 'left', 'right'); | |
var StrLit = valueObject('value'); | |
var NumLit = valueObject('value'); | |
var BoolLit = valueObject('value'); | |
var UndefinedLit = valueObject(); | |
console.log(new Let('brian', [], StrLit('Brian McKenna'), NumLit(22)) instanceof Let); // true | |
console.log(new BoolLit(true) instanceof BoolLit); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment