Created
July 16, 2012 00:34
-
-
Save hughfdjackson/3119384 to your computer and use it in GitHub Desktop.
Ugly curry adaptation, for handling `new`
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
| 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