FAQ: Advanced Objects - Factory Functions

hi,
Jake, did u get any answer , I have the same question.
thx

Why do commas throw an error in this lesson if I put them at the end of the object?

const robotFactory = (model, mobile) => {
  return {
    model: model,
    mobile: mobile,
    beep() {
      console.log(`Beep Boop`);
    }, // comma here
  }, // comma here
};

returns the error

/home/ccuser/workspace/advanced-objects-factory-functions/main.js:9
};
^

but

const robotFactory = (model, mobile) => {
  return {
    model: model,
    mobile: mobile,
    beep() {
      console.log(`Beep Boop`);
    } // no comma
  } //no comma
};

works without issue. In previous exercises, the first format works when creating objects, but now it doesn’t.

Commas can only be used inside object literals to separate properties. The last property does not need a comma.

what is the meaning of Object Instances??

const a = []
const b = new Array()
console.log(a instanceof Object, b instanceof Object)
// true true

(intanceof ) and object instances are the same? but what is the functionality of instanceof keyword?

instanceof tells us if the value we give up front is an instance of the object class on the right.

a instanceof Array    //  true

It is a tool, to confirm which class an object instance inherits from. Sting objects don’t have the same methods as Arrays, as an example.

All data types in JS are an instance of (or at least their wrapper is) inherit from a parent class. Primitives get identified and wrapped whenever an instance arises, even if only for an instance (pun intended).

Out of curiosity, how would one loop through the properties of this function? I had the following which logged three instances of ‘undefined’.

const robotFactory = (model, mobile) => {
return {
model: model,
mobile: mobile,
beep () {
console.log(‘Beep Boop’);
}
};
};

for (let prop in robotFactory(‘P-500’, true)){
console.log(robotFactory.prop);
};