1

I am trying to make a class (called Foo) like object in JavaScript that would let me create multiple instances of Foo. By multiple instances, I mean, each instance would maintain separate internal state. This is how I am achieving it.

var Foo = function() {
    function init(a) {
        this.a = a
    }

    function state() {
        return this.a
    }

    function getFoo() {
        return {
            init: init,
            state: state
        }
    }

    return {
        getFoo: getFoo
    }
}()

var a = Foo.getFoo()
var b = Foo.getFoo()
a.init(10)
b.init(20)
alert(a.state() + ', ' + b.state())

As you can see that the Foo.getFoo() seems to simulate the Factory pattern usually used in Java. The internal state is the variable this.a. I consider my attempt a success because a.state() and b.state() display two different internal states. However, this is purely my attempt without any knowledge of how these things are implemented in the industry.

Now, my questions may seem like a request for code review and is at the risk of getting closed, so I will try to make my questions as objective as possible.

  1. How can the same code be implemented such that new operator can be used instead to create the new instances of a class with this.a as the internal state?
  2. How is my code with the factory pattern written in the industry?
1

1 Answer 1

3
var Foo = function (a) {
    this.a = a;
};
Foo.prototype.state = function () {
    return this.a;
};

var a = new Foo(10);
a.state(); // 10

Bun you can also declare the state method from constructor itself like:

var Foo = function (a) {
    this.a = a;
    this.state = function () {
        return this.a;
    };
};

UPDATE: Factory pattern.

I usually use this for my factories:

var Foo = function (a) {
    this.a = a;
};
Foo.prototype.state = function () {
    return this.a;
};

var FooFactory = function () {};
FooFactory.prototype.create = function (a) {
    return new Foo(a);
};

var factory = new FooFactory();
var a = factory.create(20);
a instanceOf Foo; // true
a.state(); // 20
9
  • It doesn't help you, does it? Aug 22, 2013 at 6:08
  • Don't say that :) This is exactly what I would do. The original code isn't really a producer of Foo. It just combines some functions to make it resemble Foo. Also having the object not be the factory simplifies the code.
    – Bart
    Aug 22, 2013 at 6:15
  • @VladislavQulin It does help. I'll be accepting this answer in a couple of days once I wrap my mind around these concepts. :-) Aug 22, 2013 at 6:16
  • var FooFactory = function () {}; FooFactory.create = function (a) { return new Foo(a); }; var a = FooFactory.create(20);
    – kayz1
    Aug 22, 2013 at 6:17
  • 1
    I work with large project with whole lot of different classes, so i prefer factory pattern. But it could be professional deformation. One for sure, you should come from your needs and don't make premature optimization. Aug 22, 2013 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.