JavaScript原生基于原型委托,可用Object.create或构造函数prototype实现轻量工厂系统:前者内存高效但不支持instanceof,后者兼顾类型识别与继承,增强型工厂还支持mixin组合。

JavaScript 原生就基于原型委托(prototype delegation),无需额外框架就能构建轻量级对象工厂系统。核心是利用 Object.create() 或构造函数的 prototype,让实例自动委托到共享原型,避免重复创建方法,同时支持灵活配置和继承。
用 Object.create + 工厂函数实现最小可行工厂
这是最直接、无构造函数污染的方式:定义一个原型对象,再用工厂函数生成委托其行为的实例。
示例:
const animalProto = {
speak() {
return `I'm ${this.name} and I ${this.sound}`;
},
move() {
return `${this.name} is moving`;
}
};
const createAnimal = (name, sound) => {
const instance = Object.create(animalProto);
instance.name = name;
instance.sound = sound;
return instance;
};
const dog = createAnimal('Buddy', 'bark');
const cat = createAnimal('Luna', 'meow');
console.log(dog.speak()); // "I'm Buddy and I bark"
console.log(cat.move()); // "Luna is moving"
console.log(dog.__proto__ === animalProto); // true
✅ 所有实例共享方法,内存高效;❌ 无法用 instanceof 检测类型(因无构造函数)。
立即学习“Java免费学习笔记(深入)”;
带类型识别的构造函数工厂(推荐日常使用)
保留 instanceof 能力,同时保持原型复用——只需把方法挂到构造函数的 prototype 上,工厂函数负责初始化属性。
Java项目代码review工具。分析Git变更+完整调用链路上下文,推断业务需求,进行多维度评分和分类汇总,生成完整PRD文档。包含细粒度Java代码审查清单(Null安全、异常处理、Streams、并发、equals/hashCode、资源管理、API设计、性能、MyBatis/ORM、事务边界、SQL/DD...
示例:
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
Animal.prototype.speak = function() {
return `I'm ${this.name} and I ${this.sound}`;
};
Animal.prototype.move = function() {
return `${this.name} is moving`;
};
// 工厂函数封装构造逻辑
const createAnimal = (name, sound) => new Animal(name, sound);
// 可扩展子类工厂
function Dog(name, breed) {
Animal.call(this, name, 'bark');
this.breed = breed;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.fetch = function() {
return `${this.name} fetched the ball`;
};
const createDog = (name, breed) => new Dog(name, breed);
✅ 支持 instanceof、可继承、方法复用;✅ 工厂函数隐藏构造细节,便于统一初始化或注入依赖。
支持配置化与组合的增强型工厂
当需要动态混入行为(如可行走、可飞行、可存储状态),可用“原型委托链 + 属性合并”实现轻量组合。
示例(行为模块 + 工厂组装):
const Flyable = {
fly() { return `${this.name} is flying`; }
};
const Swimmable = {
swim() { return `${this.name} is swimming`; }
};
const Stateful = {
init(state) { this._state = { ...this._state, ...state }; },
get state() { return this._state; }
};
const createEntity = (baseProto, mixins = [], options = {}) => {
const instance = Object.create(baseProto);
// 合并 mixin 方法(浅委托)
mixins.forEach(mixin => {
Object.assign(instance, mixin);
});
// 初始化属性
Object.assign(instance, options);
// 确保状态模块被激活
if (mixins.includes(Stateful)) {
instance.init(options.state || {});
}
return instance;
};
// 使用
const bird = createEntity(Animal.prototype, [Flyable, Stateful], {
name: 'Eagle',
sound: 'screech',
state: { altitude: 1000 }
});
console.log(bird.speak()); // "I'm Eagle and I screech"
console.log(bird.fly()); // "Eagle is flying"
console.log(bird.state); // { altitude: 1000 }
⚠️ 注意:Object.assign(instance, mixin) 是浅拷贝方法到实例自身(非委托),若追求纯委托,应改用 Object.setPrototypeOf(instance, Object.assign(Object.create(baseProto), mixin)),但会略增复杂度。
关键要点总结
- 原型委托本质是
[[Prototype]]链查找,不是“复制”,所有实例共享原型方法 - 工厂函数不等于类——它只是封装了
Object.create或new的调用,解耦创建逻辑 - 避免在工厂里重复定义函数(如
() => {}),否则失去委托优势 - 需要类型检查时优先用构造函数 +
prototype;仅需行为复用且无类型需求时,Object.create更轻量 - 组合优于继承:用 mixin 原型片段 + 工厂装配,比深层继承链更可控、易测试

















