zoukankan      html  css  js  c++  java
  • js中的Object.seal()与Object.freeze()

      关键字:seal, freeze, property descriptor

    1、Object.seal()

      参考文档(2)中这样描述:

    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.

     主要就是两点:

    1. 阻止添加新属性;
    2. 现有属性变得non-configurable.

    第一点好理解;第二点需要搞清楚non-configurable是什么意思,configurable在这篇博文中有详细解释,

    这里再贴一遍:

    当configurable设为false时,

      1、不可以通过delete去删除该属性从而重新定义属性;

      2、不可以转化为访问器属性;

      3、configurable和enumerable不可被修改;

      4、writable可单向修改为false,但不可以由false改为true;

      5、value是否可修改根据writable而定。

    理解了configurable的意思,也就能理解为啥会有这个句话了:被封的对象仍可能可以修改对象的属性值。

    Values of present properties can still be changed as long as they are writable.

     2、Object.freeze()

    参考文档(3)中这样描述:

    The Object.freeze() method freezes an object. A frozen object can no longer be changed;

    freezing an object prevents new properties from being added to it,

    existing properties from being removed,

    prevents changing the enumerability, configurability, or writability of existing properties,

    and prevents the values of existing properties from being changed.

    In addition, freezing an object also prevents its prototype from being changed.

      此函数“冰冻”对象本身以及一切现有的属性值(value)以及属性的特性(property descriptor)。

    在函数Object.seal()中也许还可以修改属性值以及修改 属性的特性writable(true-->false),

    但是在Object.freeze()中,这些都干不了。

    参考文档:

    (1)https://www.cnblogs.com/tlz888/p/10389532.html

    (2)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal

    (3)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

  • 相关阅读:
    python读取csv数据(添加列名,指定分隔方式)
    loc_survived
    数据预处理
    hadoop 指令
    pd.concat
    DataFrame
    SQL左连接
    mysql mysql之把查询的结果保存到新表(小知识点)
    啦啦啦啦 mysql 授权
    ArrayList和LinkedList的区别以及优缺点
  • 原文地址:https://www.cnblogs.com/tlz888/p/10385162.html
Copyright © 2011-2022 走看看