function createPerson(name) {
const person = Object.create(createPerson.prototype);
person.name = name;
return person;
}
createPerson.prototype.greet = function() {
return `Hello, I'm ${this.name}`;
};
const john = createPerson('John');
This code demonstrates the Factory pattern with prototype: 1) It creates objects without using the 'new' keyword (unlike constructor pattern), 2) It explicitly links the new object to a prototype object, 3) It initializes the object with properties, 4) It returns the newly created object, 5) Methods are shared via the prototype for memory efficiency, 6) It combines the benefits of factory functions (no 'new' required) with prototype-based method sharing. This pattern provides flexibility while maintaining the performance benefits of prototype-based method sharing.