← 返回完整题库
JavaScript中等

Reflect 的作用是什么?

参考答案

ES6 提供的反射 API,把语言内部操作([[Get]]、[[Set]]、[[Construct]] 等)暴露为函数:

Reflect.get/set/has/deleteProperty/ownKeys/getPrototypeOf/setPrototypeOf/defineProperty/apply/construct/getOwnPropertyDescriptor/isExtensible/preventExtensions

作用

  1. 与 Proxy 配套:handler 里用 Reflect.* 执行默认行为,避免 target[key] 在 getter/原型场景下 this 指向错误;
  2. 统一返回布尔而非抛错(如 Reflect.defineProperty 返回 false 而不抛 TypeError);
  3. Reflect.construct 支持 newTarget 参数,实现更灵活的继承;
  4. Reflect.apply 更函数式,可配合 Function.prototype.apply;
  5. Reflect.ownKeys 一次性取字符串 + Symbol 键。
const p = new Proxy(t, { get(t, k, r) { return Reflect.get(t, k, r); } });

本题来自 计算机知识面试题库,共 921 道题。