0

What is the simplest/cleanest way to implement Factory pattern (Not Abstract Factory) in JavaScript? Which covers following things:

  • Performs repeating operations when setting up similar objects
  • Offers a way for the customers of the factory to create objects without knowing the specific type (class) at compile time
  • Which will work in all browsers.

I saw Factory Pattern in Addy Osmaniy's blog but it does not covers second part. Classes should be predefined.

P.S. I saw answer in this question Factory Pattern in JavaScript, but this person asks about his implementation approach which is quite difficult to read and answer also suggests to use 'Object.create' static method which is available in ES5.

3
  • I'm scared you'll have to use eval for the second part ...
    – Florian F.
    Jan 21, 2014 at 10:12
  • There is no other then eval approach?
    – Khamidulla
    Jan 21, 2014 at 10:13
  • None that i know of, if you want to implement a correct factory pattern. Matter is about referencing inheriters into the parent, and i don't think js is capable of it natively.
    – Florian F.
    Jan 21, 2014 at 10:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.