A factory function creates a configured object without exposing setup details to callers.

factory function A factory function returns a new object that already has the data and methods it needs.

Factory Functions

startValue
factory.ts
Replay: real traced execution (multi-file project)
type Counter = {
    increment(): void;
    value(): number;
};

function createCounter(start: number): Counter {
    let current: number = start;
    return {
        increment(): void {
            current = current + 1;
        },
        value(): number {
            return current;
        },
    };
}

const startValue: number = 2;
const counter: Counter = createCounter(startValue);

counter.increment();
counter.increment();

console.log(`value=${counter.value()}`);
type Counter = {
    increment(): void;
    value(): number;
};

function createCounter(start: number): Counter {
    let current: number = start;
    return {
        increment(): void {
            current = current + 1;
        },
        value(): number {
            return current;
        },
    };
}

const startValue: number = 0;
const counter: Counter = createCounter(startValue);

counter.increment();
counter.increment();

console.log(`value=${counter.value()}`);
type Counter = {
    increment(): void;
    value(): number;
};

function createCounter(start: number): Counter {
    let current: number = start;
    return {
        increment(): void {
            current = current + 1;
        },
        value(): number {
            return current;
        },
    };
}

const startValue: number = 5;
const counter: Counter = createCounter(startValue);

counter.increment();
counter.increment();

console.log(`value=${counter.value()}`);
  1. startValue ← 2

    15    };16}1718const startValue→ 2: number = 2;  //@startValue=0, 519const counter: Counter = createCounter(startValue2);
  2. current ← 2, start ← 2

    3    value(): number;4};56function createCounter(start2: number): Counter {7    let current→ 2: number = start→ 2;8    return {9        increment(): void {10            current = current + 1;11        },12        value(): number {13            return current;14        },15    };16}
  3. counter ← [object Object]

    18const startValue: number = 2;  //@startValue=0, 519const counter→ [object Object]: Counter = createCounter(startValue2);2021counter.increment();22counter.increment();2324console.log(`value=${counter[object Object].value()}`);
    outputvalue=4
  1. startValue ← 0

    15    };16}1718const startValue→ 0: number = 0;19const counter: Counter = createCounter(startValue0);
  2. current ← 0, start ← 0

    3    value(): number;4};56function createCounter(start0: number): Counter {7    let current→ 0: number = start→ 0;8    return {9        increment(): void {10            current = current + 1;11        },12        value(): number {13            return current;14        },15    };16}
  3. counter ← [object Object]

    18const startValue: number = 0;19const counter→ [object Object]: Counter = createCounter(startValue0);2021counter.increment();22counter.increment();2324console.log(`value=${counter[object Object].value()}`);
    outputvalue=2
  1. startValue ← 5

    15    };16}1718const startValue→ 5: number = 5;19const counter: Counter = createCounter(startValue5);
  2. current ← 5, start ← 5

    3    value(): number;4};56function createCounter(start5: number): Counter {7    let current→ 5: number = start→ 5;8    return {9        increment(): void {10            current = current + 1;11        },12        value(): number {13            return current;14        },15    };16}
  3. counter ← [object Object]

    18const startValue: number = 5;19const counter→ [object Object]: Counter = createCounter(startValue5);2021counter.increment();22counter.increment();2324console.log(`value=${counter[object Object].value()}`);
    outputvalue=7