Python's __getattr__ in Javascript

Viewed 6555

Is there a way to simulate Python's __getattr__ method in Javascript?

I want to intercept 'gets' and 'sets' of Javascript object's properties.

In Python I can write the following:

class A:
    def __getattr__(self, key):
        return key

a = A()
print( a.b )  # Output: b

What about Javascript?

2 Answers
Related