{"action":"created","issue":{"url":"https://api.github.com/repos/andrewplummer/Sugar/issues/472","labels_url":"https://api.github.com/repos/andrewplummer/Sugar/issues/472/labels{/name}","comments_url":"https://api.github.com/repos/andrewplummer/Sugar/issues/472/comments","events_url":"https://api.github.com/repos/andrewplummer/Sugar/issues/472/events","html_url":"https://github.com/andrewplummer/Sugar/issues/472","id":47669417,"number":472,"title":"Feature Request: Set/Get Object with 'Dot Notation'","user":{"login":"DylanPiercey","id":4985201,"avatar_url":"https://avatars.githubusercontent.com/u/4985201?v=3","gravatar_id":"","url":"https://api.github.com/users/DylanPiercey","html_url":"https://github.com/DylanPiercey","followers_url":"https://api.github.com/users/DylanPiercey/followers","following_url":"https://api.github.com/users/DylanPiercey/following{/other_user}","gists_url":"https://api.github.com/users/DylanPiercey/gists{/gist_id}","starred_url":"https://api.github.com/users/DylanPiercey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DylanPiercey/subscriptions","organizations_url":"https://api.github.com/users/DylanPiercey/orgs","repos_url":"https://api.github.com/users/DylanPiercey/repos","events_url":"https://api.github.com/users/DylanPiercey/events{/privacy}","received_events_url":"https://api.github.com/users/DylanPiercey/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/andrewplummer/Sugar/labels/Feature","name":"Feature","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":16,"created_at":"2014-11-04T02:00:42Z","updated_at":"2015-09-30T15:27:21Z","closed_at":null,"body":"    var setProp = function (obj, path, val) {\r\n        path = path.split('.');\r\n    \r\n        for (var i = 0, max = path.length - 1; i < max; i += 1) {\r\n            obj = obj[path[i]] = obj[path[i]] || {};\r\n        }\r\n    \r\n        obj[path[i]] = val;\r\n    };\r\n    \r\n    var getProp = function (obj, path) {\r\n        path = path.split('.');\r\n    \r\n        for (var i = 0, max = path.length; i < max; i += 1) {\r\n            obj = obj[path[i]];\r\n        }\r\n    \r\n        return obj;\r\n    };\r\n\r\nWhich (when extending Object) would let us do:\r\n\r\n    var obj = {};\r\n    Object.setProp(obj, 'location.address.street', 'test');\r\n    console.log(obj.location.address.street); // -> test\r\n\r\nAnd retrieve it dynamically with this:\r\n\r\n    console.log(Object.getProp(obj, 'location.address.street')); // -> test\r\n"},"comment":{"url":"https://api.github.com/repos/andrewplummer/Sugar/issues/comments/144446157","html_url":"https://github.com/andrewplummer/Sugar/issues/472#issuecomment-144446157","issue_url":"https://api.github.com/repos/andrewplummer/Sugar/issues/472","id":144446157,"user":{"login":"andrewplummer","id":74646,"avatar_url":"https://avatars.githubusercontent.com/u/74646?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewplummer","html_url":"https://github.com/andrewplummer","followers_url":"https://api.github.com/users/andrewplummer/followers","following_url":"https://api.github.com/users/andrewplummer/following{/other_user}","gists_url":"https://api.github.com/users/andrewplummer/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewplummer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewplummer/subscriptions","organizations_url":"https://api.github.com/users/andrewplummer/orgs","repos_url":"https://api.github.com/users/andrewplummer/repos","events_url":"https://api.github.com/users/andrewplummer/events{/privacy}","received_events_url":"https://api.github.com/users/andrewplummer/received_events","type":"User","site_admin":false},"created_at":"2015-09-30T15:27:21Z","updated_at":"2015-09-30T15:27:21Z","body":"OK more developments here. For reasons maybe a bit too complicated to go into here, `get` and `set` cannot be set on `Object.prototype`. Sugar does not modify `Object.prototype` by default, but there is a mode that allows type checking methods (`isNumber`), etc and hash methods (`keys`, etc) to be opted-in. It's a bit \"at your own risk\" but it does add value. There are essentially two ways to handle this:\r\n\r\n1. Simply don't allow the method on on the prototype.\r\n\r\nThe static method would still work, as well as on extended objects:\r\n\r\n```\r\nObject.get(obj, 'prop');\r\nvar extended = Object.extended(obj);\r\nextended.get('prop');\r\n```\r\n\r\n2. Rename the methods.\r\n\r\nEither by adding to both:\r\n\r\n```\r\nObject.getProp(obj, 'prop');\r\nObject.setProp(obj, 'prop', 'val');\r\nobj.getProp('prop'); // Note this won't work normally\r\nobj.setProp('prop', 'val'); // Note this won't work normally\r\n```\r\n\r\nOr making a single method that acts as both a getter and a setter as suggestion above:\r\n\r\n```\r\nObject.prop(obj, 'prop');\r\nObject.prop(obj, 'prop', 'val');\r\nobj.prop('prop'); // Note this won't work normally\r\nobj.prop('prop', 'val'); // Note this won't work normally\r\n```\r\n\r\nPersonally I'm leaning toward simply forbidding it on `Object.prototype`. This mode of usage isn't meant to be standard anyway and extended objects are still available, so this seems to be the cleanest way."}}