-
Notifications
You must be signed in to change notification settings - Fork 2k
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
object slice/reconstruction: pulling selected properties from an object #1708
Comments
+1 |
under this scheme would we also have
compile to
? |
@MichaelBlume: I don't think so. Like an object literal, only things that qualify as a |
👍 |
Giving this the 👍 that I thought I had already given it: 👍 This is one of my favorite things from coco. As much as I disagree with his perl-over-ruby philosophy, I think @satyr has put a lot of great ideas into coco. C: |
+1 |
I intend to implement this within the next 3 weeks. edit: make that 5 edit again: ugh, school. I'll get to this as soon as I can. |
+1 |
I found myself wishing for this today as a way of copying certain known properties from configuration objects and such: class Thing
@allowed_attributes = [ 'id', 'name', 'updated_at' ]
constructor: (attributes = {}) ->
@attributes = attributes{@allowed_attributes...}
# or if you want to keep it static
@attributes = attributes{id, name, updated_at} Is this a feature that could make it in given a suitable PR? Edit: coco has this as 'object slice'. |
There is a (imo) better syntax proposal in #3225, otherwise these are duplicates, I'd move the discussion there and close this one. |
From what I can see, #3225 is slightly different and talks only about destructuring. This seems to be discussing the inverse (restructuring?) case. a = {}
a{b, c} = d
# VS
a = d{b, c} The analog for arrays would be splicing vs slicing ( |
I take your point, however I'd rather have both than one or the other, as they seem more complementary than competing. My comparison with arrays was definitely a little flawed, it's more like the difference between:
And:
|
Is not what the syntax denotes. d = 'adam'
a = [1, 2, 3, 4]
a[0..2] = d
console.log a # ['adam', 4] |
Oh, it seems I've misunderstood that syntax (and/or the splice function) for some time, sorry. Thanks for correcting me! I guess, then, since there is no such parity between That said, I still like the simplicity offered by the latter, though I admittedly can't think of any situation where the former could not be used instead (with the required |
A PR for this would be welcome. |
I would like to propose a syntax similar to one in #1632 for creating an object from specified properties of another object. This syntax would solve the same problem that the proposal in #1617 is trying to solve (and I briefly mentioned over there).
The only downside that I can find shows itself when you need to reference a dynamic property name:
It's still a lot better than the current alternative. Though, we should probably only allow those dynamic properties if we also bring back dynamic property names in object literals.
This syntax is already available in coco, so you can try it out and see example compilations by using cup. So, thanks @satyr.
The text was updated successfully, but these errors were encountered: