Skip to content

Instantly share code, notes, and snippets.

@hughfdjackson
Created July 16, 2012 00:34
Show Gist options
  • Select an option

  • Save hughfdjackson/3119384 to your computer and use it in GitHub Desktop.

Select an option

Save hughfdjackson/3119384 to your computer and use it in GitHub Desktop.
Ugly curry adaptation, for handling `new`
Function.prototype.curry = function () {
var slice = Array.prototype.slice,
args = slice.apply(arguments),
that = this;
return function _curried() {
var fn = Function.prototype.bind.apply(that, args.concat(slice.apply(arguments)));
fn.prototype = that.prototype;
if ( this instanceof _curried ) return new fn();
else return fn();
};
};
var Field = function (type, name, description) {
this.name = name;
this.description = description;
this.type = type;
};
Field.prototype.as_html_field = function () {
var widget;
switch(this.type) {
case 'char':
widget = document.createElement('input');
widget.setAttribute('type', 'text');
break;
case 'int':
widget = document.createElement('input');
widget.setAttribute('type', 'number');
break;
case 'text':
widget = document.createElement('textarea');
break;
default:
return 'failed';
}
widget.setAttribute('name', this.name);
widget.setAttribute('placeholder', this.name);
return widget;
}
var CharField = Field.curry('char');
var TextField = Field.curry('text');
var IntField = Field.curry('int');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment