Intercept object attribute access in JavaScript -
this question has answer here:
i want able intercept access attribute object has not been set in javascript. wonder if it's possible?
the equivalent in python __getattr__
built-in method:
class p(object): def __getattr__(self, name): return name p = p() x = p.x
p.x doesn't exist, __getattr__
intercept access member variable has not been created. similar in javascript?
you able proxies. example mdn:
var handler = { get: function(target, name){ return name in target? target[name] : 37; } }; var p = new proxy({}, handler); p.a = 1; p.b = undefined; console.log(p.a, p.b); // 1, undefined console.log('c' in p, p.c); // false, 37
however, currently browser support non-existent , polyfilling not possible.
Comments
Post a Comment