Created
May 16, 2019 20:32
-
-
Save PetrSnobelt/921dd6d75881b836dfca521cfe9c5598 to your computer and use it in GitHub Desktop.
JS Proxy logger
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 getLogger = (prefix = "") => ({ | |
get: function(zdroj, vlastnost){ | |
console.log(prefix, "try access", vlastnost); | |
if(vlastnost in zdroj){ | |
return zdroj[vlastnost]; | |
} else { | |
return 37; | |
} | |
}, | |
defineProperty: function (oTarget, sKey, oDesc) { | |
console.log("defineProperty", sKey, oDesc) | |
if (oDesc && "value" in oDesc) { | |
oTarget[sKey] = oDesc.value; | |
} | |
return oTarget; | |
}, | |
}); | |
var getDeepLogger = (prefix = "") => ({ | |
get: function(zdroj, vlastnost) { | |
if (vlastnost in zdroj) { | |
var result = zdroj[vlastnost] | |
if (typeof result === "object") { | |
return new Proxy(result, getDeepLogger(prefix + "." + vlastnost)); | |
} else { | |
console.log(prefix, vlastnost) | |
return result; | |
} | |
} else { | |
console.log(prefix, "try access undefined property", vlastnost); | |
return undefined; | |
} | |
}, | |
defineProperty: function (oTarget, sKey, oDesc) { | |
console.log("defineProperty", sKey, oDesc) | |
if (oDesc && "value" in oDesc) { | |
oTarget[sKey] = oDesc.value; | |
} | |
return oTarget; | |
}, | |
}); | |
var p = new Proxy({a:{b:{c:1}}}, getLogger("P proxy")); | |
var c = new Proxy({a:{b:{c:1}}}, getDeepLogger("C proxy")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment