Hacker News new | past | comments | ask | show | jobs | submit login

My JS knowledge isn't great. In python, I can do operator overloading such that `obj[item] = value` does something completely different than assignment. Is that possible with JavaScript?



You can set a custom getters and setters, but I wouldn't call it operator overloading. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

So...

    > var x = {}
    > Object.defineProperty(x, 'value', { get: function() { return 1 }, set: function() { console.log('set') } })
    > x.value
    1
    > x.value = 2
    set
    > x.value
    1




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: