Last active
August 29, 2015 14:04
-
-
Save westmark/f49a073a1705418e0c65 to your computer and use it in GitHub Desktop.
Object property wrapper
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
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