Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created February 2, 2013 20:07
Show Gist options
  • Save puffnfresh/4699039 to your computer and use it in GitHub Desktop.
Save puffnfresh/4699039 to your computer and use it in GitHub Desktop.
Automatic creation of constructors.
// 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