在阅读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
从上面我们可以看出:
- 被封闭对象仍旧全等该对象本身
- 可以通过
Object.isSealed
来判断当前对象是否被封闭 - 不能为被封闭对象添加任何未知属性, 也不能为其已知属性添加访问者
- 可以修改已知的属性
再看下面例子
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
网友评论