-
Notifications
You must be signed in to change notification settings - Fork 2k
advanced object construction: obj.{ prop0: a, prop1: b } or similar #1632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I do like the |
somewhat related: #1708 |
note: This issue corresponds to the es-next proposal harmony:object_literals#object_extension_literal. An example of its usefulness can be seen at harmony:object_extension_literal_class_pattern |
My 2¢ -- the most natural syntax for object-extension in Coffeescript is splat-notation: |
I agree. I would love to copy Coco's object splats. Its advantages outweigh its drawbacks. |
I seems Coco uses a different syntax for object splats ( a = {b:2, c:3}
b = {a:1, ...:a, d:4} var a, b, ref$;
a = {
b: 2,
c: 3
};
b = (ref$ = {
a: 1
}, import$(ref$, a), ref$.d = 4, ref$);
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
} I kinda like these semantics (regardless of where the splats should be). But i don't know how useful an addition it would be. One case that i'd say is pretty common: merging two objects, does not look particularly sexy with this syntax: |
@epidemian: In the example I gave above, merging of 2 or more objects actually looks particularly sexy to me: |
Because Coco's splat operator is prefix (as in ES6).
This form is added later to support splat in braceless objects, e.g.:
|
+1. If I could only vote for one CS addition, it would be this one. |
Thank's to @jashkenas referencing me to "previous tickets discussing exactly the same thing" #3468, I read through the issue titles and found this. Consider this example of transferring two properties from
it could be made much cleaner by coffee script, as well as not needing to create an object from
Deconstructing is allowed, but transferring and picking is just as prevalent if not more. This is something I've always needed underscore for and have been thinking the syntax could be more readable. the proposed 2 cases would output something like
|
So what is the status on this? I see this PR has been open for a long time, and it appears that everybody thinks it's a good idea to add something like this to CS. Is this a 'PR welcome' kind of situation, is there something in the works somewhere? |
It's getting to be that time... |
Yes, this is a ‘PR welcome’ situation 😄 |
I wanted to start a discussion about the syntax mentioned in the title and originally on es-discuss. It produces a modified clone of an object. So I'd like to hear whether people would have a use for it, how well it will play with other features, and exactly how we'd like it to compile/behave. I'd recommend at least reading the linked thread from es-discuss.
I believe the suggested syntax
obj.{ a: b, c: d }
is equivalent to Coco's{...obj, a: b, c: d}
, which is another possible syntax we can consider. Coco's syntax actually has a few advantages over the one above:{a: b, ...o, c: d, e: f}
{...defaults, ...o, ...overrides}
Though a negative of that syntax is the fact that it forces us to define merging semantics; something that's always a problem. But Coco's always-overwrite with only own-keys seems like a pretty sane choice.
The text was updated successfully, but these errors were encountered: