JavaScript design Pattern---(Factory mode learning)

Source: Internet
Author: User

The Factory mode (Factory) provides a common interface to create objects.

If you have a UI library, we want to create a type of UI component that does not need to be created directly using the new operator or through another create-type constructor, but rather requires the factory object to create a new component. We tell factory what kind of object (such as a button, panel) it needs, it instantiates it, and then returns it to us.

functionCar (name, color) { This. Name =name;  This. color =color;}functionTrunk (name, color) { This. Name =name;  This. color =color;}functionvehiclefactory () {}vehiclefactory.prototype.vehicleclass= Car;//Add default TypeVehicleFactory.prototype.createVehicle =function(name, color) {if(name = = = ' car ')) {         This. Vehicleclass =Car; } Else {         This. Vehicleclass =Trunk; }    return Newvehicleclass (name, color);} Let Factory=Newvehiclefactory () Let car= factory.createvehicle (' car ', ' red ') Console.log (carinstanceofCar)//Truelet trunk= factory.createvehicle (' trunk ', ' blank ') Console.log (trunkinstanceofTrunk)//True

JavaScript design Pattern---(Factory mode learning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.