首页 > 编程语言 >Javascript Object 中,isExtensible/isSealed/isFrozen 的对比

Javascript Object 中,isExtensible/isSealed/isFrozen 的对比

时间:2024-03-04 14:25:05浏览次数:25  
标签:isSealed non isExtensible false frozen Object object isFrozen

目录

isExtensible

extensible object 的定义:
they can have new properties added to them, and their [[Prototype]] can be re-assigned. An object can be marked as non-extensible using one of Object.preventExtensions(), Object.seal(), Object.freeze(), or Reflect.preventExtensions().

示例如下:

// New objects are extensible.
const empty = {};
Object.isExtensible(empty); // true

// They can be made un-extensible
Object.preventExtensions(empty);
Object.isExtensible(empty); // false

// Sealed objects are by definition non-extensible.
const sealed = Object.seal({});
Object.isExtensible(sealed); // false

// Frozen objects are also by definition non-extensible.
const frozen = Object.freeze({});
Object.isExtensible(frozen); // false




Object.isExtensible(1);
// TypeError: 1 is not an object (ES5 code)

Object.isExtensible(1);
// false                         (ES2015 code)

isSealed

An object is sealed if it is not extensible and if all its properties are non-configurable and therefore not removable (but not necessarily non-writable).

示例如下:

// Objects aren't sealed by default.
const empty = {};
Object.isSealed(empty); // false

// If you make an empty object non-extensible,
// it is vacuously sealed.
Object.preventExtensions(empty);
Object.isSealed(empty); // true

// The same is not true of a non-empty object,
// unless its properties are all non-configurable.
const hasProp = { fee: "fie foe fum" };
Object.preventExtensions(hasProp);
Object.isSealed(hasProp); // false

// But make them all non-configurable
// and the object becomes sealed.
Object.defineProperty(hasProp, "fee", {
  configurable: false,
});
Object.isSealed(hasProp); // true

// The easiest way to seal an object, of course,
// is Object.seal.
const sealed = {};
Object.seal(sealed);
Object.isSealed(sealed); // true

// A sealed object is, by definition, non-extensible.
Object.isExtensible(sealed); // false

// A sealed object might be frozen,
// but it doesn't have to be.
Object.isFrozen(sealed); // true
// (all properties also non-writable)

const s2 = Object.seal({ p: 3 });
Object.isFrozen(s2); // false
// ('p' is still writable)

const s3 = Object.seal({
  get p() {
    return 0;
  },
});
Object.isFrozen(s3); // true
// (only configurability matters for accessor properties)




Object.isSealed(1);
// TypeError: 1 is not an object (ES5 code)

Object.isSealed(1);
// true                          (ES2015 code)

isFrozen

An object is frozen if and only if it is not extensible, all its properties are non-configurable, and all its data properties (that is, properties which are not accessor properties with getter or setter components) are non-writable.

示例如下:

// A new object is extensible, so it is not frozen.
Object.isFrozen({}); // false

// An empty object which is not extensible
// is vacuously frozen.
const vacuouslyFrozen = Object.preventExtensions({});
Object.isFrozen(vacuouslyFrozen); // true

// A new object with one property is also extensible,
// ergo not frozen.
const oneProp = { p: 42 };
Object.isFrozen(oneProp); // false

// Preventing extensions to the object still doesn't
// make it frozen, because the property is still
// configurable (and writable).
Object.preventExtensions(oneProp);
Object.isFrozen(oneProp); // false

// Deleting that property makes the object vacuously frozen.
delete oneProp.p;
Object.isFrozen(oneProp); // true

// A non-extensible object with a non-writable
// but still configurable property is not frozen.
const nonWritable = { e: "plep" };
Object.preventExtensions(nonWritable);
Object.defineProperty(nonWritable, "e", {
  writable: false,
}); // make non-writable
Object.isFrozen(nonWritable); // false

// Changing that property to non-configurable
// then makes the object frozen.
Object.defineProperty(nonWritable, "e", {
  configurable: false,
}); // make non-configurable
Object.isFrozen(nonWritable); // true

// A non-extensible object with a non-configurable
// but still writable property also isn't frozen.
const nonConfigurable = { release: "the kraken!" };
Object.preventExtensions(nonConfigurable);
Object.defineProperty(nonConfigurable, "release", {
  configurable: false,
});
Object.isFrozen(nonConfigurable); // false

// Changing that property to non-writable
// then makes the object frozen.
Object.defineProperty(nonConfigurable, "release", {
  writable: false,
});
Object.isFrozen(nonConfigurable); // true

// A non-extensible object with a configurable
// accessor property isn't frozen.
const accessor = {
  get food() {
    return "yum";
  },
};
Object.preventExtensions(accessor);
Object.isFrozen(accessor); // false

// When we make that property non-configurable it becomes frozen.
Object.defineProperty(accessor, "food", {
  configurable: false,
});
Object.isFrozen(accessor); // true

// But the easiest way for an object to be frozen
// is if Object.freeze has been called on it.
const frozen = { 1: 81 };
Object.isFrozen(frozen); // false
Object.freeze(frozen);
Object.isFrozen(frozen); // true

// By definition, a frozen object is non-extensible.
Object.isExtensible(frozen); // false

// Also by definition, a frozen object is sealed.
Object.isSealed(frozen); // true




Object.isFrozen(1);
// TypeError: 1 is not an object (ES5 code)

Object.isFrozen(1);
// true                          (ES2015 code)

示意图

标签:isSealed,non,isExtensible,false,frozen,Object,object,isFrozen
From: https://www.cnblogs.com/dongling/p/18051716

相关文章

  • java Object类
    javaObject类Object包含了9大常用方法:getClass()hashcode()toString()equals()finalize()notify()notifyAll()wait()clone()  一、toString()  方法摘要  publicStringtoString():返回该对象的字符串表示。  toString方法返回该对象的字符串表示,其实该字符串内......
  • 9_dynamic动态类型与object类型的区别
    C#中dynamic动态类型与object类型的区别1.object类型object类型是.NET中System.Object类的别名。在C#中,所有类型(预定义、用户定义、引用类型、值类型)都直接或间接继承自Object。因此,我们可以将任何类型的值用object对象来接收。2.dynamic动态类型动态......
  • Halcon HObject和C#的Bitmap图像互转
    转载:https://blog.51cto.com/u_15088375/3247784#:~:text=HalconHObject和C%23的Bitmap图像互转1HalconHObject和C%23的Bitmap图像互转2一,Bitmap转HObject3方法一:HOperatorSet.GenImageInterleaved直接转换4方法二:C%23获取图像各个通道内存首地址和HOperatorSet.GenIma......
  • python面向对象(Object Oriented Program)
    面向对象(ObjectOrientedProgram)在Python中,所有的数据(包括数字和字符串)实际都是对象,同一类型的对象都有相同的类型。我们可以使用type()函数来获取关于对象的类型信息。什么是类,什么是对象?在Python中,使用类来定义同一种类型的对象。类(class)是广义的数据类型,能够定义复......
  • Qt 多线程中使用QTimer和信号、槽 QObject::startTimer: Timers cannot be started fr
    多线程中使用QTimer我们可能在Qt的多线程中使用QTimer中都会遇到一个错误:Cannotcreatechildrenforaparentthatisinadifferentthread.或者QObject::startTimer:TimerscannotbestartedfromanotherthreadQTimer定时器不能在不同的线程中启动。出现这个主要原因......
  • redis自学(7)RedisObject
    RedisObjectredis中的任意数据类型的键和值都会封装为一个RedisObject,也叫做Redis对象。    Redis对象头部占16个字节(不包含指针指向的内存空间),所以大量数据使用的情况下,不推荐使用OBJ_STRING类型存储,用OBJ_LIST更好Redis的编码方式   ......
  • Python scipy.ndimage.find_objects用法及代码示例
    用法scipy.ndimage.find_objects(input,max_label=0)在标记数组中查找对象。参数:input:整数数组包含由不同标签定义的对象的数组。值为0的标签将被忽略。max_label:整数,可选要在输入中搜索的最大标签。如果没有给出max_label,则返回所有对象的位置。object_slices:元组......
  • ssts-hospital-web-master项目实战记录十四:项目迁移-模块实现(file-system-object:FileS
    记录时间:2024-02-25一、准备工作【使用“文心一言”搜索使用ts实现类模块】在TypeScript中,类可以作为模块的一部分被导出,这样其他模块就可以导入并使用这些类。以下是如何使用TypeScript实现类模块的基本步骤:步骤1:定义类模块首先,在一个TypeScript文件中定义一个或多个......
  • 【Unity】ScriptableObject使用之后,重新打开Unity会报错
    导致问题出现的原因:ScriptableObject的类与其他类放一起了具体请见:unity2020.3官方文档ScriptableObject部分解决方法:为ScriptableObject单独创建一个c#脚本,并将c#名称与ScriptableObject类名保持一致。猜测出现这样的原因,如果哪里不对烦请大佬指正一下:Unity中基本上都是会通......
  • ssts-hospital-web-master项目实战记录九:父组件App.vue给子组件main-page*.vue传值ter
    记录时间:2024-02-23一、App.vue详见:App.vue组件实现 【使用“文心一言”提取组件传值逻辑说明和代码】在Vue3中,组件传值通常涉及到props、emits、slots和组件的引用。在提供的代码中,主要关注点是动态组件的渲染和通过props传递数据。下面是对组件传值逻辑的说明和代码......