-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Features: Support splats in object literals (object rest/spread syntax) #66
Comments
I've been seeing object spread being recommended and I've seen it being used heavily in large production code bases. I find myself using I think this does require you to use the fun a: 1, b... # ambiguous
fun {a: 1, b...} # unambiguous Also do we go with the spec and put the operator on the left One benefit to having it go in the opposite direction would be that it could disambiguate the two and we could get rid of the need for # object spread:
fun a: 1, ...b # fun({ a: 1, ...b })
# argument spread:
fun a: 1, b... # fun({ a: 1 }, ...b) Though consistency of the spread syntax might be preferred over getting rid of ambiguity. |
I'd definitely vote for consistency over that ambiguity, though it is an annoying one. Ultimately I suppose it's consistent with needing explicit braces to have shorthand objects in arguments lists too. f a: a, b # f({ a: a }, b)
f { a: a, b } # f({ a: a, b: b }) |
I'd recommend to put the so you can spread an object from the function like this (This is very common in Vuex, Redux): export default {
name: 'CounterBlock'
methods:
...mapActions
increment : counter.actions.increment,
set : counter.actions.set,
fetchCount: counter.actions.fetchCount
computed:
...mapGetters
total: counter.getters.total
} currently with export default {
name: 'CounterBlock'
methods: Object.assign {}, mapActions
increment : counter.actions.increment,
set : counter.actions.set,
fetchCount: counter.actions.fetchCount
computed: Object.assign {}, mapGetters
total: counter.getters.total
} with export default {
name: 'CounterBlock',
methods: {
...mapActions({
increment : counter.actions.increment,
set : counter.actions.set,
fetchCount: counter.actions.fetchCount
})
},
computed: {
...mapGetters ({
total: counter.getters.total
})
},
} |
Added via jashkenas/coffeescript#4493 |
@GeoffreyBooth @YamiOdymel
|
@chabib Well, if I'm not mistaken, you could use it like this. object = {
...mapActions
increment : counter.actions.increment,
set : counter.actions.set,
fetchCount: counter.actions.fetchCount
} Which produces: var object, _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
object = _extends({}, mapActions({
increment: counter.actions.increment,
set: counter.actions.set,
fetchCount: counter.actions.fetchCount
})); is the same as below with object = {
...mapActions({
increment : counter.actions.increment,
set : counter.actions.set,
fetchCount: counter.actions.fetchCount
})
} |
Thank you @YamiOdymel !
But not this
|
@chabib I asked about this in jashkenas/coffeescript#4449
|
Migrated to jashkenas/coffeescript#4959 |
I would love to see object spreads and rests supported via splats in object literals:
Compilation in the
2
branch could happily useObject.assign
for construction:Destructuring would require more implementation, e.g.
We could alternatively wait for the proposal to land in environments, or rely on a transpiler like babel.
The text was updated successfully, but these errors were encountered: