美文网首页前端修仙之路
Object.seal方法理解

Object.seal方法理解

作者: 朝西的生活 | 来源:发表于2018-11-26 10:51 被阅读0次

    在阅读react源码时注意到react在创建ref时使用了Object.seal来保证RefObject的一致性, 具体如下

    /* ReactCreateRef.js */
    // an immutable object with a single mutable value
    export function createRef(): RefObject {
      const refObject = {
        current: null,
      };
      if (__DEV__) {
        Object.seal(refObject);
      }
      return refObject;
    }
    

    摘自MDN:

    The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

    简单来说, Object.seal()方法被用来封闭一个对象,阻止添加新属性并将所有现有属性标记为不可配置。当前属性的值只要可写就可以改变, 并且返回一个被密封的对象的引用.

    看看MDN上面的一些例子:

    var obj = {
      prop: function() {},
      foo: 'bar'
    };
    
    // New properties may be added, existing properties
    // may be changed or removed.
    obj.foo = 'baz';
    obj.lumpy = 'woof';
    delete obj.prop;
    
    var o = Object.seal(obj);
    
    o === obj; // true
    Object.isSealed(obj); // === true
    
    // Changing property values on a sealed object
    // still works.
    obj.foo = 'quux';
    
    // But you can't convert data properties to accessors,
    // or vice versa.
    Object.defineProperty(obj, 'foo', {
      get: function() { return 'g'; }
    }); // throws a TypeError
    
    // Now any changes, other than to property values,
    // will fail.
    obj.quaxxor = 'the friendly duck';
    // silently doesn't add the property
    delete obj.foo;
    // silently doesn't delete the property
    
    // ...and in strict mode such attempts
    // will throw TypeErrors.
    function fail() {
      'use strict';
      delete obj.foo; // throws a TypeError
      obj.sparky = 'arf'; // throws a TypeError
    }
    fail();
    
    // Attempted additions through
    // Object.defineProperty will also throw.
    Object.defineProperty(obj, 'ohai', {
      value: 17
    }); // throws a TypeError
    Object.defineProperty(obj, 'foo', {
      value: 'eit'
    }); // changes existing property value
    

    从上面我们可以看出:

    1. 被封闭对象仍旧全等该对象本身
    2. 可以通过Object.isSealed来判断当前对象是否被封闭
    3. 不能为被封闭对象添加任何未知属性, 也不能为其已知属性添加访问者
    4. 可以修改已知的属性

    再看下面例子

    var a = {b: 1, c: 2, d: {e: 3}}
    Object.seal(a)
    a.d.f = 4;
    a.d
    {e: 3, f: 4}
    

    可以看出: 与Object.freeze一样, Object.seal方法一样, Object.seal并不是递归的, 在封闭一个对象之后, 我们仍然可以访问/设置其下一级的属性.

    参考: MDN

    相关文章

      网友评论

        本文标题:Object.seal方法理解

        本文链接:https://www.haomeiwen.com/subject/yhpjqqtx.html