Created
June 13, 2014 11:16
-
-
Save hkan/555d79173c4ecf18e31b to your computer and use it in GitHub Desktop.
Object Prototype
This file contains hidden or 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
Object.defineProperty(Object.prototype, "setDotNotated", { | |
value: function (dotNotation, value) { | |
var arrayOfNames = dotNotation.split('.'); | |
var last = arrayOfNames.pop(); | |
var tmp = this; | |
for (var i in arrayOfNames) { | |
if (!arrayOfNames.hasOwnProperty(i)) | |
continue; | |
if (!tmp[arrayOfNames[i]]) | |
tmp[arrayOfNames[i]] = {}; | |
tmp = tmp[arrayOfNames[i]]; | |
} | |
tmp[last] = value; | |
return this; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment