Created
August 15, 2012 08:42
DRY generators
This file contains 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
// original | |
var methods = { | |
front: function (col) { | |
return this.each(function () { | |
$(this).css('color', col || plugin.statics.random()); | |
}); | |
}, | |
back: function (col) { | |
return this.each(function () { | |
$(this).css('background-color', col || plugin.statics.random()); | |
}); | |
} | |
}; | |
// DRY | |
var methods = {}; | |
var generate = function(key) { | |
return function(col) { | |
return this.each(function () { | |
$(this).css(key, col || plugin.statics.random()); | |
}); | |
}; | |
}; | |
var hooks = {"front": "color", "back": "background-color"}; | |
for (var i in hooks) { | |
methods[i] = generate(hooks[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, and thanks :) that's a good idea in most cases! but in these example files the focus is on the modplug API, it should be as easy to understand as possible. I'm afraid it could scare off people if it is harder to understand..