FAQ: Advanced Objects - Factory Functions

It has to do with const. Using const prevents the name from being reassigned. If we used the arrow syntax, but declared the function with let we could still accidentally reassign the name of the function, and thereby lose access to the original function.

1 Like

Factory functions is an interesting concept. Before learning factory functions, I would have done something like this,

function robotFactory(model,mobile){
this.model = model;
this.mobile = mobile;
this.beep = function(){
console.log(“Beep Boop”)
}
}

tinCan = new robotFactory(“P-500”,true)
tinCan.beep()

Are there any glaring advantages of disadvantages to using factory functions over this method?

Isn’t that outdated because of the classes, introduced in ECMAScript 2015?

Talking about Factory Functions, how can use the factory to create objects inside other objects??

like in:

Restaurant={
create serve1
create serve2
create serve3

create serve500
}

I have a question about creating ojects with a Factory function. I see that you can still use the function without first assigning it to a variable, like this:

robotFactory('HAL', true);

But this way the oject doesn’t have a name to identify it, right? Can I call this object afterwards then? Is it some kind of anonymous object?

In a way, yes. The object in that form can be passed as the argument to another function. Note that it would still be assigned to a variable, the parameter of that function.

hiya, weird one this:

what would be some good ways to make different robots have different beeps? right now theyre all stuck with the same noise.
I’m ashamed to say I was about to try experimenting but failed pretty quick.

maybe just a hint or a scent to follow?

2 Likes

Hi, basically, just make sure you add a third parameter to the robotFactory function, call this ‘sound’, then, you want to store that sound, so make a property called with key ‘sound’ and add value ‘sound’. Then, when logging the sound, simply log the variable ‘sound’. Here is my code:
const robotFactory = (model, mobile,sound) => {

return{

model: model,

mobile: mobile,

sound: sound,

beep(){

  console.log(sound)

}

}

};

const tinCan = robotFactory(‘P-500’,true, ‘BAAAAP’);

tinCan.beep();

1 Like

I get no errors but can’t pass part 1.

const robotFactory = (model, mobile) => {
  return { 
    model: model,
    mobile: mobile, 
    beeb() {
      console.log('Beep Boop');
    } 
  }
};

Edit: beeb should be beep

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

I like to start curly brackets in the next line, makes things more clear and readable to me, and it works fine for everything else, however the curly brackets after the return must be in the same line.

const robotFactory = (model, mobile) =>
{
  return { // IMPORATNT: Do NOT put the curly bracket after a return in a separate line!
    model: model,
    mobile: mobile,
    beep()
    {
      console.log("Beep Boop")
    }
  }
};

The above works, but if I put the curly bracket after the return in its own line, I get SyntaxError: Unexpected token : for the colon after mobile, which is just all kinds of puzzling to me, as apparently it didnt give an error for the colon after model which comes before. Can anybody explain this to me?

return must appear on the same line as the expression it is returning. Multi-line expressions such as objects are common, as you have above. Technically, it is on the same line as the return keyword (in memory).

1 Like

const simply ensures that someone cannot reassign a value to this variable which currently has an arrow function as its value

catchPhrase is simply a value we pass in as an argument. We can do this as a factory function is a function meaning it can take in arguments. This value will then be logged to the console every time we call the scare() method


@technologiclee const` simply ensures that someone cannot reassign a value to this variable which currently has an arrow function as its value

Hello Mtf

In this lesson, everything is ok , it printed ok but i just want to know why i cannot put ’ ’ (string for true parameter ) whereas ‘P-500’ is used ’ ’ ?

const tinCan = robotFactory(‘P-500’,true);

thanks

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

const tinCan = robotFactory(‘P-500’,true);
tinCan.beep();
console.log(tinCan.model);
console.log(tinCan.mobile);

true and false are booleans, not strings. They are never written in quotes.

1 Like

thanks, mtf , i thought only numbers, well noted

1 Like

if i want to add new model then i have to make new variable every time, right ?? and how can i access the object that saved different key properties ?

In most cases, yes, so we can tell them apart and keep them unique. Above you have two instances, each with their own variable. They can be used separately or together.

There is one situation where we might only ever use one variable which is when the instance is overwritten or consumed (destroyed). But that is not the case here. We can still update our instances by changing the values of the existing properties.

tincan.model = 'P-5500'

There, we just gave our robot an upgrade.