I don't think you should use this pattern, and I think you'll have trouble making it work. Consider the following cases:
// calls the position setter on foo
foo.position = {x: 10, y: 20};
// calls the position getter on foo, obtaining a simple {x:10,y:20} object
var pos = foo.position;
// calls the position getter on foo and then the x getter on position
var xx = foo.position.x;
So far, everything makes sense. Then we get to this situation:
// calls the position getter on foo and then the x setter on position
foo.position.x = 7;
Since we returned a simple map with the position getter, position.x simply assigns to the returned copy and doesn't modify foo's actual position. One way to fix this would be to have the position getter return a smarter object that has a reference to the foo instance and proper getter/setters. This would allow doing:
foo.position = bar.position;
bar.x += 10;
The bar.position
getter would return an object that merely acts as a view for bar
's x/y properties. Then, the foo.position
setter would copy bar
's x/y properties into its private storage. Makes sense. bar.x += 10
ads only to bar
's position.
However, the following situation would be very confusing:
var temp = foo.position;
foo.position = bar.position;
bar.position = temp;
This creates the illusion that temp is a backup copy of foo.position
but it isn't. It's a view. As soon as foo's position changes, we lose the data for good. There's no easy way to copy the position.
You could try making your position
objects actually store both a copy of the data as well as a reference to the original, so that you can do both set and get... It's an endless problem.
But at this point, your sugar syntax is making everything much harder to maintain as well as much less efficient.
So instead of doing all this, I'd actually make is so that the x and y getter/setters are on the foo/bar objects themselves. I'd also make any code inside the foo/bar object treat positions as if they were immutable.