我试图得到这样的东西:
// instead of having to write
a.b = 'c'; return a;
// i'd like to write
return a.b = 'c';
// but this will return a.b instead of a
// a (cumbersome) alternative is
return Object.defineProperty(a, 'b', {value: 'c',
enumerable: true,
writable: true,
configurable: true})
// or using a shorthand function
function set(target, property, value){
return Object.defineProperty(target, property, {value: value,
enumerable: true,
writable: true,
configurable: true});
}
return set(a, 'b', 'c');
我会对某种重载 the=
甚至.
运算符的方式更感兴趣,但我不知道这是否可能或如何做到这一点。