Search code examples
javascriptecmascript-next

JavaScript - Object.assign to Spread operator


I initialize my component with this constructor :

constructor($q, $state) {
  'ngInject'
  Object.assign(this, { $q, $state })
}

the simple equivalent of

this.$q = $q
this.$state = $state

What is it alternative with Spread operator ?


Solution

  • None. You can only use spread syntax when creating a new object with literal syntax. You don't do that when you use a constructor.

    Btw, there is no spread "operator", and object spread syntax is not part of ES6.