Factory Function in Javascript

 Factory Function in Javascript

In this article we are goanna to see about Factory Function in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.



A factory function is any function which is not a class or constructor that returns an object. In JavaScript, any function can return an object. When it does so without the new keyword, it’s a factory function.

Example:

function mobile( ){

return { 

model: “mi 9 pro”,

price: function( ){ return (“Price 3000”);}

};

}

var redmi = mobile( );

document.write(redmi.model + redmi.price( ));


Example:

function mobile(model_name ){

return { 

model : model_name;

price : function( ){ return (“Price 3000”);}

};

}

var redmi = mobile(“redmi pro 9” );

var nokia = mobile(“nokia 3000” );

document.write(redmi.model + redmi.price( ));


Share:

0 comments

Please leave your comments...... Thanks