Skip to content

Instantly share code, notes, and snippets.

@westmark
Last active August 29, 2015 14:04
Show Gist options
  • Save westmark/f49a073a1705418e0c65 to your computer and use it in GitHub Desktop.
Save westmark/f49a073a1705418e0c65 to your computer and use it in GitHub Desktop.
Object property wrapper
var Prototype = {
}
var Wrap = function () {
var mappedAttributes = {};
_.each(arguments, function (a) {
if (a && a.length) {
var i = 1
k = a.substring(0, i);
while (mappedAttributes.hasOwnProperty(k)) {
k = a.substring(0, ++i);
}
mappedAttributes[k] = a;
}
});
var Wrapped = function (attributes) {
this.attributes = attributes || {};
};
_.extend(Wrapped.prototype, Prototype, _.zipObject(_.map(mappedAttributes, function (long, short) {
return [long, function (value) {
if (_.isUndefined(value)) {
return this.attributes[short];
}
this.attributes[short] = value;
return this;
}];
})));
return Wrapped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment