reflect 本身不能直接调用父类原型方法,需显式通过 parent.prototype 获取方法,再用 reflect.apply 绑定 this 并执行;动态方法名或 symbol 时,用 reflect.get + reflect.apply 更安全灵活。

在继承场景中,用 Reflect 本身并不能直接“调用父类原型方法”,因为 Reflect 提供的是对底层操作的反射式访问(如 get、apply、construct 等),它不负责方法查找或原型链遍历。真正实现“调用父类原型方法”的核心仍是传统方式:明确指定父类的原型对象,再结合 Reflect.apply 或普通 call/apply 来绑定 this 并执行。
明确获取父类原型上的方法
ES6 类继承中,子类构造函数的 super 指向父类构造函数,而父类的原型方法存在于 Parent.prototype 上。要调用它,需先从该对象上取到方法引用:
- 用
Parent.prototype.methodName显式获取方法 - 不能依赖
this.methodName,否则可能被子类重写覆盖 - 若方法名是动态的(如字符串变量),可用
Reflect.get(Parent.prototype, methodName)
用 Reflect.apply 调用并绑定 this 和参数
拿到方法后,用 Reflect.apply 可以安全、统一地调用,并显式控制 this 和参数列表:
class Parent {
greet() {
return `Hello from ${this.name}`;
}
}
class Child extends Parent {
constructor(name) {
super();
this.name = name;
}
sayHi() {
// ✅ 正确:从 Parent.prototype 获取 greet,再用 Reflect.apply 调用
return Reflect.apply(Parent.prototype.greet, this, []);
}
}
等价于 Parent.prototype.greet.call(this),但 Reflect.apply 更适合参数为数组或需统一调用逻辑的场景。
处理动态方法名和 Symbol 方法
当方法名不确定或含 Symbol 时,Reflect.get + Reflect.apply 组合更灵活:
-
const method = Reflect.get(Parent.prototype, 'greet')—— 支持字符串或 Symbol 键 -
if (typeof method === 'function') { Reflect.apply(method, this, args) }—— 安全调用前校验类型 - 避免
Parent.prototype[methodName].call(this)在键不存在时抛错
注意 super() 和 super.xxx 的本质
super 不是对象,而是语法关键字,其行为由 JS 引擎在内部绑定当前对象的原型链。在子类方法中写 super.greet(),引擎实际做了类似 Reflect.get(Object.getPrototypeOf(Child.prototype), 'greet').call(this) 的事。但你无法在运行时“复刻”完整的 super 行为(比如自动跳过子类自身定义的方法),所以手动指定 Parent.prototype 是最可靠的方式。
不复杂但容易忽略:Reflect 是工具,不是继承机制的替代品;调用父类方法的关键,在于找准目标原型对象,再用合适方式执行。
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南











