Javascript - 不能冻结和密封的东西

时间:2013-12-08 15:18:02

标签: javascript object

为什么我不能分别封存和冻结o.id,o.id.name,o.id.surname?。

var o = Object.defineProperty({}, "id", 
          {value: {name:{value:"Eduardo",writable:true,enumerable:true,configurable:true},
               surname:{value:"Valente", writable:true,enumerable:true,configurable:true}},
              writable:true,enumerable:true,configurable:true});
      Object.preventExtensions(o.id);
      console.assert(Object.isExtensible(o.id), "o.id is not extensible");
      Object.seal(o.id);
      console.assert(Object.isSealed(o.id), "o is not sealed");
      console.log(JSON.stringify(Object.getOwnPropertyDescriptor(o, "id")));
      Object.freeze(o.id.name);
      Object.freeze(o.id.surname);
      console.assert(Object.isFrozen(o.id.name), "o.id.name is not frozen");
      console.assert(Object.isFrozen(o.id.surname), "o.id.surname is not frozen");
      console.log(JSON.stringify(Object.getOwnPropertyDescriptor(o, "id")));

      function display(o)
      {
         o["id"]["name"] = "Felipe";
         o["id"]["surname"] = "Badia";
         console.log(o["id"]["name"]);
         console.log(o["id"]["surname"]);
      }    

      display(o);

1 个答案:

答案 0 :(得分:0)

您正在密封类似属性描述符的对象,这些对象是o.ido.value属性的初始值。

这并不妨碍您重新分配这些属性以指向其他值。

冻结对象与可能包含它们的任何属性的可写性无关。