Intercept object attribute access in JavaScript -


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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -