Assign properties from object
solova opened this issue · 11 comments
Docs contains good example:
class Person
constructor: (options) ->
{@name, @age, @height} = options
It's short and clear.
When I want to assign variables from object to locat variables it's work too:
{_name, _age, _height} = options
But If I want assign variables from one object to another I get error:
{obj.name, obj.age, obj.height} = options
I think It's not issue. But this fall short of expectations.
I suggest to add this functionality. I'm pretty much sure it's very useful.
For example in Angular:
{$scope.prevPage, $scope.nextPage} = response
Of couse I can use parallel assignment:
[$scope.prevPage, $scope.nextPage] = [response.prevPage, response.nextPage]
but it's longest and produced redundancy code.
This is where LiveScript becomes a very handy alternative 😉 :
obj <<< options{name, age, height}compiles to:
obj.name = options.name;
obj.age = options.age;
obj.height = options.height;Another LiveScript alternative (that i find a bit more intuitive):
obj{name, age, height} = optionsOf course @epidemian, you're right, I just forgot about this form! ^^ And this is perfectly matching the semantics of what @xelios20 wants.
for methods / function it can looks like
class Person
constructor: ({@name, @age, @height} ) ->
but have solution like:
obj{name, age, height} = options
would be nice
I think that this is an extension worth having, if anyone wants to take a stab at it. The general idea would be to take the current destructuring assignment, and then allow it to be prefixed:
obj{a, b} = c
array[1].value[a, b, c] = otherArray
Note that there will be some tricky bits for compilation, that would need to be cached:
thing.lookupOther(key(param)){name: [a, b, {value: c}]} = otherThing
But now that I'm writing that above example ... it also seems like the potential for totally garbled and hard to parse syntax here is pretty high. Do folks think this is useful enough to add? Or potentially way too complicated to read to be worth having?
I really like it in LS. most useful with extend(a, b.{c, d})
From #1952 thread:
{prevPage: $scope.prevPage, nextPage: $scope.nextPage} = response # works now
$scope{prevPage, nextPage} = response # should work
{prevPage: $scope.a, nextPage: $scope.b} = response # works now
$scope{prevPage: a, nextPage: b} = response # should workOh my goodness, obj{a, b} = c would be very useful. My peers and I have longed for it many, many times. Excited that it's ("finally!" =P) being considered!
+1
+1
If someone wants to implement this, a pull request would be welcome.