var obj = {}
obj.__setitem__ = function(key, value){
  this[key] = value * value
}
obj.x = 2  // 4
obj.y = 3  // 9

JavaScript doesn't have __setitem__ and this example obviously doesn't work.

In python __setitem__ works like:

class CustomDict(dict):
  def __setitem__(self, key, value):
    super(CustomDict, self).__setitem__(key, value * value)

d = CustomDict()
d['x'] = 2  # 4
d['y'] = 3  # 9

Is it possible to implement __setitem__ behavior in JavaScript? All tricky workarounds would be helpful.

Edit
Report