Skip to content
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

Assign properties from object #3225

Closed
solova opened this issue Oct 31, 2013 · 11 comments
Closed

Assign properties from object #3225

solova opened this issue Oct 31, 2013 · 11 comments

Comments

@solova
Copy link

solova commented Oct 31, 2013

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.

@ymeine
Copy link

ymeine commented Oct 31, 2013

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;

@epidemian
Copy link
Contributor

Another LiveScript alternative (that i find a bit more intuitive):

obj{name, age, height} = options

@ymeine
Copy link

ymeine commented Oct 31, 2013

Of course @epidemian, you're right, I just forgot about this form! ^^ And this is perfectly matching the semantics of what @xelios20 wants.

@justgook
Copy link

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

@jashkenas
Copy link
Owner

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?

@vendethiel
Copy link
Collaborator

I really like it in LS. most useful with extend(a, b.{c, d})

@xixixao
Copy link
Contributor

xixixao commented Nov 16, 2013

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 work

@aseemk
Copy link
Contributor

aseemk commented Nov 17, 2013

Oh 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!

@RonaldJerez
Copy link

+1

@SJAnderson
Copy link

+1

@GeoffreyBooth
Copy link
Collaborator

If someone wants to implement this, a pull request would be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests