Home > OS >  Extract object values from constructor function
Extract object values from constructor function

Time:11-12

How can i extract these values from my object? Without writing 4 lines

constructor(product) {
    { title, imgUrl, price, desc } = product
}

CodePudding user response:

Deconstruction works this way also:

constructor({ title, imgUrl, price, desc }) {}

CodePudding user response:

You could try

var product = null;

function getProduct(){
  return this.product
}

constructor(product) {
  this.product =  { title, imgUrl, price, desc }
}

In the other class you could instance this class and call the getProduct() function

  • Related