• ES6知识点整理之----Reflect


    1、与Proxy对象一样,也是 ES6 为了操作对象而提供的新 API。Reflect对象的设计目的有这样几个。

    • Object对象的一些明显属于语言内部的方法(比如Object.defineProperty),放到Reflect对象上。
    • 修改某些Object方法的返回结果,让其变得更合理。
    • Object操作都变成函数行为。
    • Reflect对象的方法与Proxy对象的方法一一对应,只要是Proxy对象的方法,就能在Reflect对象上找到对应的方法。这就让Proxy对象可以方便地调用对应的Reflect方法,完成默认行为,作为修改行为的基础。

    2、静态方法

    共有 13 个静态方法。

    Reflect.get(target, name, receiver)

    查找并返回target对象的name属性,如果没有该属性,则返回undefined

    var myObject = {
      foo: 1,
      bar: 2,
      get baz() {
        return this.foo + this.bar;
      },
    }
    
    Reflect.get(myObject, 'foo') // 1
    Reflect.get(myObject, 'bar') // 2
    Reflect.get(myObject, 'baz') // 3

    如果name属性部署了读取函数(getter),则读取函数的this绑定receiver

    如果第一个参数不是对象,Reflect.get方法会报错。

    Reflect.set(target, name, value, receiver)

    设置target对象的name属性等于value

    var myObject = {
      foo: 1,
      set bar(value) {
        return this.foo = value;
      },
    }
    
    myObject.foo // 1
    
    Reflect.set(myObject, 'foo', 2);
    myObject.foo // 2
    
    Reflect.set(myObject, 'bar', 3)
    myObject.foo // 3

    注意,如果 Proxy 对象和 Reflect 对象联合使用,前者拦截赋值操作,后者完成赋值的默认行为,而且传入了receiver,那么Reflect.set会触发Proxy.defineProperty拦截。

    如果第一个参数不是对象,Reflect.set会报错。

    Reflect.has(obj, name)

    Reflect.has方法对应name in obj里面的in运算符。

    如果第一个参数不是对象,Reflect.hasin运算符都会报错。

    var myObject = {
      foo: 1,
    };
    
    // 旧写法
    'foo' in myObject // true
    
    // 新写法
    Reflect.has(myObject, 'foo') // true

    Reflect.deleteProperty(obj, name)

    等同于delete obj[name],用于删除对象的属性。返回一个布尔值。如果删除成功,或者被删除的属性不存在,返回true;删除失败,被删除的属性依然存在,返回false

    const myObj = { foo: 'bar' };
    
    // 旧写法
    delete myObj.foo;
    
    // 新写法
    Reflect.deleteProperty(myObj, 'foo');

    Reflect.construct(target, args)

    等同于new target(...args),这提供了一种不使用new,来调用构造函数的方法。

    function Greeting(name) {
      this.name = name;
    }
    
    // new 的写法
    const instance = new Greeting('张三');
    
    // Reflect.construct 的写法
    const instance = Reflect.construct(Greeting, ['张三']);

    Reflect.getPrototypeOf(obj)

    用于读取对象的__proto__属性,对应Object.getPrototypeOf(obj)

    Reflect.getPrototypeOfObject.getPrototypeOf的一个区别是,如果参数不是对象,Object.getPrototypeOf会将这个参数转为对象,然后再运行,而Reflect.getPrototypeOf会报错。

    const myObj = new FancyThing();
    
    // 旧写法
    Object.getPrototypeOf(myObj) === FancyThing.prototype;
    
    // 新写法
    Reflect.getPrototypeOf(myObj) === FancyThing.prototype;

    Reflect.setPrototypeOf(obj, newProto)

    用于设置目标对象的原型(prototype),对应Object.setPrototypeOf(obj, newProto)方法。它返回一个布尔值,表示是否设置成功。

    const myObj = {};
    
    // 旧写法
    Object.setPrototypeOf(myObj, Array.prototype);
    
    // 新写法
    Reflect.setPrototypeOf(myObj, Array.prototype);
    
    myObj.length // 0

    如果无法设置目标对象的原型(比如,目标对象禁止扩展),Reflect.setPrototypeOf方法返回false

    如果第一个参数不是对象,Object.setPrototypeOf会返回第一个参数本身,而Reflect.setPrototypeOf会报错。

    如果第一个参数是undefinednullObject.setPrototypeOfReflect.setPrototypeOf都会报错。

    Reflect.apply(func, thisArg, args)

    等同于Function.prototype.apply.call(func, thisArg, args),用于绑定this对象后执行给定函数。

    const ages = [11, 33, 12, 54, 18, 96];
    
    // 旧写法
    const youngest = Math.min.apply(Math, ages);
    const oldest = Math.max.apply(Math, ages);
    const type = Object.prototype.toString.call(youngest);
    
    // 新写法
    const youngest = Reflect.apply(Math.min, Math, ages);
    const oldest = Reflect.apply(Math.max, Math, ages);
    const type = Reflect.apply(Object.prototype.toString, youngest, []);

    Reflect.defineProperty(target, propertyKey, attributes)

    基本等同于Object.defineProperty,用来为对象定义属性。

    function MyDate() {
      /**/
    }
    
    // 旧写法
    Object.defineProperty(MyDate, 'now', {
      value: () => Date.now()
    });
    
    // 新写法
    Reflect.defineProperty(MyDate, 'now', {
      value: () => Date.now()
    });

    如果Reflect.defineProperty的第一个参数不是对象,就会抛出错误

    这个方法可以与Proxy.defineProperty配合使用

    Reflect.getOwnPropertyDescriptor(target, propertyKey)

    基本等同于Object.getOwnPropertyDescriptor,用于得到指定属性的描述对象

    var myObject = {};
    Object.defineProperty(myObject, 'hidden', {
      value: true,
      enumerable: false,
    });
    
    // 旧写法
    var theDescriptor = Object.getOwnPropertyDescriptor(myObject, 'hidden');
    
    // 新写法
    var theDescriptor = Reflect.getOwnPropertyDescriptor(myObject, 'hidden');

    如果第一个参数不是对象,Object.getOwnPropertyDescriptor(1, 'foo')不报错,返回undefined,而Reflect.getOwnPropertyDescriptor(1, 'foo')会抛出错误,表示参数非法。

    Reflect.isExtensible (target)

    对应Object.isExtensible,返回一个布尔值,表示当前对象是否可扩展。

    const myObject = {};
    
    // 旧写法
    Object.isExtensible(myObject) // true
    
    // 新写法
    Reflect.isExtensible(myObject) // true

    如果参数不是对象,Object.isExtensible会返回false,因为非对象本来就是不可扩展的,而Reflect.isExtensible会报错。

    Reflect.preventExtensions(target)

    对应Object.preventExtensions方法,用于让一个对象变为不可扩展。它返回一个布尔值,表示是否操作成功。

    var myObject = {};
    
    // 旧写法
    Object.preventExtensions(myObject) // Object {}
    
    // 新写法
    Reflect.preventExtensions(myObject) // true

    如果参数不是对象,Object.preventExtensions在 ES5 环境报错,在 ES6 环境返回传入的参数,而Reflect.preventExtensions会报错。

    Reflect.ownKeys (target)

    用于返回对象的所有属性,基本等同于Object.getOwnPropertyNamesObject.getOwnPropertySymbols之和。

    var myObject = {
      foo: 1,
      bar: 2,
      [Symbol.for('baz')]: 3,
      [Symbol.for('bing')]: 4,
    };
    
    // 旧写法
    Object.getOwnPropertyNames(myObject)
    // ['foo', 'bar']
    
    Object.getOwnPropertySymbols(myObject)
    //[Symbol(baz), Symbol(bing)]
    
    // 新写法
    Reflect.ownKeys(myObject)
    // ['foo', 'bar', Symbol(baz), Symbol(bing)]
  • 相关阅读:
    iOS 10 因苹果健康导致闪退 crash-b
    iOS10 配置须知-b
    iOS开发 适配iOS10以及Xcode8-b
    iOS 10 的适配问题-b
    mybatis中分页查询
    mybatis开发流程,增删改查
    spring mvc接收参数方式,json格式返回请求数据
    xml配置文件中常见的命名空间解释
    myeclipse中配置schemaLocation路径,实现xml文件自动提示
    spring MVC工作流程
  • 原文地址:https://www.cnblogs.com/adhehe/p/9675537.html
Copyright © 2020-2023  润新知