{configurable:false} 或 Object.seal() 工作不正常

{configurable:false} or Object.seal() is not working correctly

我正在尝试学习 javascript,并且发现我们可以使用对象 属性 的属性。 (我的意思是 valuewritableenumerableconfigurable ).

据我所知,我认为更改 {configurable: false} 会限制任何更多的配置更改,例如 {writable: false, enumerable: false }

我在下面写了试一试,但我得到的结果与我预期的结果完全不同。

我对 {configurable:false} 的理解有误吗?或者,代码有问题吗? TIA.

"use strict";

window.onload = function(){

  var o = {x:1};

  //Make "x" non-configurable
  Object.defineProperty(o, "x", {configurable: false});
  //Seal "o";
  Object.seal(o);

  console.log(Object.getOwnPropertyDescriptor(o, "x"));
  //outputs => Object { value: 1, writable: true, enumerable: true, configurable: false }
  console.log(Object.isSealed(o));
  //outputs => true

  Object.defineProperty(o, "x", {writable: false}); //this doesn't cause any errors.
  console.log(Object.getOwnPropertyDescriptor(o, "x"));
  //outputs => Object { value: 1, writable: false, enumerable: true, configurable: false }
}

MDN - seal

Configurable attribute
The configurable attribute controls at the same time whether the property can be deleted from the object and whether its attributes (other than writable to false) can be changed

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.

你需要freeze

MDN - freeze

Comparison to Object.seal() Objects sealed with Object.seal() can have their existing properties changed. Existing properties in objects frozen with Object.freeze() are made immutable.

"use strict";

window.onload = function(){

  var o = {x:1};

  //Make "x" non-configurable
  Object.defineProperty(o, "x", {configurable: false});
  //freeze "o";
  Object.freeze(o);

  console.log(Object.getOwnPropertyDescriptor(o, "x"));
  //outputs => Object { value: 1, writable: true, enumerable: true, configurable: false }
  console.log(Object.isSealed(o));
  //outputs => true

  Object.defineProperty(o, "x", {writable: true}); //Now this doesn't cause.
  console.log(Object.getOwnPropertyDescriptor(o, "x"));
  //outputs => Object { value: 1, writable: false, enumerable: true, configurable: false }
}