-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakOn.js
42 lines (33 loc) · 1.13 KB
/
breakOn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function breakOn(obj, propName) {
var props = propName.split('.'),
lastIndex = props.length - 1,
propValues = [];
handleProp(obj, 0);
function handleProp(curObj, i) {
var curProp = props[i],
hasProperty;
if (curObj && typeof curObj === 'object') {
hasProperty = curObj.hasOwnProperty(curProp);
if (hasProperty)
propValues[i] = i ? propValues[i - 1][curProp] : curObj[curProp];
if (!hasProperty || typeof curObj[curProp] !== 'object' || !curObj[curProp] || i === lastIndex)
define(curObj, i);
}
if (i < lastIndex)
handleProp(propValues[i], ++i);
}
function define(obj, i) {
Object.defineProperty(obj, props[i], {
get: function () {
return propValues[i];
},
set: function (val) {
if (i === lastIndex)
debugger;
propValues[i] = val;
if (val && typeof val === 'object')
define(val, i + 1)
}
});
}
}