Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Created August 15, 2012 08:42
DRY generators
// 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]);
}
@lrsjng
Copy link

lrsjng commented Aug 15, 2012

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment