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
Sign up to request clarification or add additional context in comments.

9 Comments

It doesn't help you, does it?
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.
@VladislavQulin It does help. I'll be accepting this answer in a couple of days once I wrap my mind around these concepts. :-)
var FooFactory = function () {}; FooFactory.create = function (a) { return new Foo(a); }; var a = FooFactory.create(20);
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.