Can't use space in properties of Factory Function?

Hi

I discovered that you can’t use spaces when declaring a property + parameter for factory functions? I’ve tried single / double / backticks for “power supply” but none of them work, MSDN doesn’t seem to say anything about it as well.

The following will get an error:
SyntaxError: Invalid destructuring assignment target

Solution:
just get rid of the space and quotes

const pcGaming = (model, color, "power Supply", cost) => {
  return {
    model: 'itx',
    color: 'black',
    "power Supply": '3000 watts',
    cost: '50usd'
  }
}

const amdGaming = pcGaming('amd3060', 'black', '2000watts', '40usd');
console.log(amdGaming);

Yes; you can’t use strings as parameters, since they’re meant to be placeholders, not actual values.

what’s confusing me is that we can have spaces in object properties, which I believe is also a placeholder?

Can you show me an example, please? I haven’t encountered that before

This is valid as a property name given it is in quotes. Property names are not placeholders; they have an associated value.

Parameters on the other hand don’t have an associated value until the caller supplies an argument. If values are used in the parameter list they must be defaults.

(a, b, c=1)

Now to speak of the elephant in the room, the unused parameters. We will have to resolve your issue.

const pcGaming = (model, color, powerSupply, cost) => {
  return {
    model: model,
    color: color,
    "power Supply": powerSupply,
    cost: cost
  }
}
const itxGaming = pcGaming('itx', 'black', '3000watts', '50usd');
console.log(amdGaming);

Extra Study

Destructuring Assignment

 > pcGaming = (model, color, psu, cost) => {
     return {
       model,
       color,
       psu,
       cost
     }
   }
<- (model, color, psu, cost) => {
     return {
       model,
       color,
       psu,
       cost
     }
   }
 > itxGaming = pcGaming('itx', 'black', '3000watts', '50usd')
<- {model: 'itx', color: 'black', psu: '3000watts', cost: '50usd'}

So long as the parameter names match the properties they are automatically assigned.

1 Like

cleared it up for me, thnx, these nuances are good to understand.

1 Like

also creating a simple test helped me really solidify the diff between placeholder vs parameter

// this will error unless you give each placeholder a value, even if it's an empty value
const testPropertyBlanks = {
  fName: 'James',
  lName:  
};

// the 2 let variables do not require a value 
const testPropertyBlanks2 = (fName2, lName2) => {
  let firstName2;
  let lastName2;
};

hope I understood it correctly but doing that helped me a lot

1 Like