JavaScript Factory Functions vs Constructor Functions vs Classes
Eric Elliott
3.3K33

There is no example for factory function so correct me if I misunderstood. do you mean something like this?

function createCar(gas) {

let location = 0;

return {

move

};

function move() {

if(gas)

location++;

}

}
(Sorry for the poor formatting)

If that is what you mean. I agree with you 80%.

this approach remove the need for “this” keyword. Also, all functions and variables declared are private by default and can only be exposed as needed in the retunr {}. This takes advantage of javascript’s beautiful closure.

If we go with constructor/class approach, we’ll be forced to have 
this.location = 0;

this.gas = gas;

then everytime we call variables, we’ll have

if(this.gas)

which is annoying.

The remaining 20% is thatI think typescript and angular 2 with visual studio code is doing a good job so far. typescript promotes classes, which I hate, but it makes IDE support easier (I think).

Like what you read? Give karlo jose serrano a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.