3 ms·
yeah, i kinda agree.. in this case I'd have done something more like: function Renderer() {} Renderer.prototype = { draw: { checkbox: functi
by taf2 13y ago
yeah, i kinda agree.. in this case I'd have done something more like:
function Renderer() {}
Renderer.prototype = {
draw: {
checkbox: function() {},
radio: function() {},
select: function() {},
field: function(type) {
this[type]();
},
list: function(items) {
return items.map(function(item) {
return this.field(item.type)
});
}
}
}
Perhaps I'm just comment trolling though - totally depends on how the code would have been used...
- chime 13y agoActually my code ends up looking pretty much like yours. I use CoffeeScript and was writing a simplified JS equivalent off the top of my head since this post was about JS. Similar to your Renderer, I have a DrawForm class that iterates through DrawField class objects which provide the checkbox(), radio() etc. methods. My point was that in the end, you get to do dynamic function instantiation, which is pretty awesome in JS.