js的Reflect对象

Reflect 对象是 JavaScript ES6 中引入的一个内建对象,它提供了一系列与对象操作相关的方法。这些方法与 Object 对象上的方法类似,但在行为上有一些差异,并且更加规范和统一。Reflect 对象并不是一个构造函数,不能被 new 操作符调用,它是一个静态对象,所有方法都是静态方法。

Reflect 对象的详细方法与示例

  1. Reflect.get(target, propertyKey[, receiver])

    • 作用: 读取对象的属性值。

    • 示例:

    const obj = {
      name: 'Alice',
      age: 25,
      get greeting() {
        return `Hello, ${this.name}!`;
      },
    };
    
    const protoObj = {
      city : 'New York'
    }
    
    Object.setPrototypeOf(obj, protoObj);
    
    console.log(Reflect.get(obj, 'name'));           // 输出: Alice (获取自身属性)
    console.log(Reflect.get(obj, 'age'));            // 输出: 25
    console.log(Reflect.get(obj, 'greeting'));      // 输出: Hello, Alice! (访问 getter)
    console.log(Reflect.get(obj, 'city'));           // 输出: New York (访问原型属性)
    
    const obj2 = {
      name: 'Bob',
    }
    
    // 使用 receiver 修改 getter 函数的 this 指向
    console.log(Reflect.get(obj, 'greeting', obj2)); // 输出: Hello, Bob!
    
  2. Reflect.set(target, propertyKey, value[, receiver])

    • 作用: 设置对象的属性值。

    • 示例:

    const obj = {
      name: 'Charlie',
      set age(newAge) {
         this._age = newAge
      },
    };
    const obj2 = {}
    Object.setPrototypeOf(obj, obj2)
    
    console.log(Reflect.set(obj, 'name', 'David')); // 输出: true
    console.log(obj.name);                       // 输出: David
    
    Reflect.set(obj, 'age', 30);  // 设置 setter
    
    console.log(obj._age); // 输出: 30
    
    Reflect.set(obj, 'age', 35, obj2) // setter 的 this 绑定为 obj2
    console.log(obj2._age); // 输出: 35
    
  3. Reflect.has(target, propertyKey)

    • 作用: 检查对象是否包含指定的属性(包括原型链上的)。
    • 示例:
    const obj = { name: 'Eve', age: 20 };
    const protoObj = {
        city : 'London'
    }
    Object.setPrototypeOf(obj, protoObj)
    console.log(Reflect.has(obj, 'name'));   // 输出: true
    console.log(Reflect.has(obj, 'age'));    // 输出: true
    console.log(Reflect.has(obj, 'city'));   // 输出: true
    console.log(Reflect.has(obj, 'address')); // 输出: false
    
  4. Reflect.deleteProperty(target, propertyKey)

    • 作用: 删除对象的属性。
    • 示例:
    const obj = { name: 'Frank', age: 40 };
    console.log(Reflect.deleteProperty(obj, 'age'));  // 输出: true
    console.log(obj.age);                             // 输出: undefined
    console.log(Reflect.deleteProperty(obj, 'city')); // 输出: true (删除不存在的属性也会返回 true)
    
  5. Reflect.construct(target, argumentsList[, newTarget])

    • 作用: 类似于使用 new 操作符调用构造函数。
    • 示例:
    class Person {
      constructor(name, age) {
        this.name = name;
        this.age = age;
      }
      greeting() {
        console.log(`Hello, ${this.name}!`);
      }
    }
    
    const args = ['Grace', 30];
    const person1 = Reflect.construct(Person, args);
    person1.greeting() // 输出: Hello, Grace!
    console.log(person1 instanceof Person);        // 输出: true
    
    // 使用 newTarget 模拟继承
    class Student extends Person {}
    const student = Reflect.construct(Person, args, Student);
    console.log(student instanceof Student) // 输出 true
    console.log(student instanceof Person) // 输出 true
    
  6. Reflect.getPrototypeOf(target)

    • 作用: 获取对象的原型。
    • 示例:
    const obj = { name: 'Henry' };
    const protoObj = { city : 'Paris' };
    Object.setPrototypeOf(obj, protoObj);
    console.log(Reflect.getPrototypeOf(obj) === protoObj); // 输出: true
    console.log(Reflect.getPrototypeOf(obj) === Object.prototype); // 输出 false
    
  7. Reflect.setPrototypeOf(target, prototype)

    • 作用: 设置对象的原型。
    • 示例:
    const obj = { name: 'Ivy' };
    const newProto = { city : 'Rome' };
    
    console.log(Reflect.setPrototypeOf(obj, newProto));    // 输出: true
    console.log(Reflect.getPrototypeOf(obj) === newProto); // 输出: true
    
  8. Reflect.apply(target, thisArgument, argumentsList)

    • 作用: 类似于使用 Function.prototype.apply() 调用函数。
    • 示例:
    function greet(greeting, punctuation) {
      return `${greeting}, ${this.name}${punctuation}`;
    }
    
    const obj = { name: 'Jack' };
    const args = ['Hello', '!'];
    const result = Reflect.apply(greet, obj, args);
    console.log(result);   // 输出: Hello, Jack!
    
  9. Reflect.defineProperty(target, propertyKey, attributes)

    • 作用: 类似于 Object.defineProperty(),用于定义对象的属性。
    • 示例:
    const obj = {};
    const attributes = {
      value: 'Katy',
      writable: true,
      enumerable: true,
      configurable: true,
    };
    
    Reflect.defineProperty(obj, 'name', attributes);
    console.log(obj.name);         // 输出: Katy
    
    // 尝试修改值
    obj.name = 'Lily';
    console.log(obj.name);         // 输出: Lily
    
  10. Reflect.ownKeys(target)

    • 作用: 获取对象的所有自身属性的键名,包括 Symbol 类型的键名。
    • 示例:
    const obj = { name: 'Mike', age: 35, [Symbol('symbolKey')]: 'symbolValue'};
    const keys = Reflect.ownKeys(obj);
    console.log(keys);   // 输出: ['name', 'age', Symbol(symbolKey)]
    
  11. Reflect.isExtensible(target)

    • 作用: 判断对象是否可扩展
    • 示例:
    const obj = {name : "Nancy"}
    console.log(Reflect.isExtensible(obj)) // 输出 true
    Reflect.preventExtensions(obj);
    console.log(Reflect.isExtensible(obj)) // 输出 false
    
  12. Reflect.preventExtensions(target)

    • 作用: 让对象不可扩展
    • 示例:
      const obj = {name : "Oliver"}
       console.log(Reflect.preventExtensions(obj)) // 输出 true
       obj.age = 30; // 设置属性无效
       console.log(obj.age); // 输出 undefined
    

总结:

Reflect 对象提供了一套统一且规范的 API 来操作对象,它的方法通常与 Proxy 对象配合使用,可以实现更加强大和灵活的元编程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值