为什么 configurable:false 允许更改可写标志但不允许 ienumerable
Why configurable:false allows to change writable flag but does'nt for enumerable
我正在编写 javascript 程序以了解将可配置标志设置为 false 如何影响 属性。程序是:
let user={
name:"heman"
};
Object.defineProperty(user, "name",{
configurable:false
});
try{
Object.defineProperty(user, "name",{
configurable:true
})
}//Throws error
catch(err){console.log(err)}
try{
Object.defineProperty(user, "name",{
enumerable:false
});
} //Throws Error
catch(err){console.log(err)}
try{
Object.defineProperty(user, "name", {
writable:false
});
} //Does'nt Throws Error. But Why?
catch(err){console.log(err)}
在这个程序中,我首先创建了一个名为 user 的对象,user 包含一个 属性 即 name:"heman"。我将名称 属性 的可配置标志设置为 false (configurable:false)。然后我再次尝试将可配置标志更改为 true 它引发了错误。在我尝试将可枚举标志更改为与其默认值相反的 false 后,它再次抛出错误。这次我试图将可写标志更改为 false。标志改变了,没有抛出任何错误。即使将可配置标志设置为 false 为什么 属性 仍然可配置?为什么可写标志发生变化而没有抛出任何错误。我想知道在以下两种情况下会发生什么:
1) 可配置标志设置为真
2) 可配置标志设置为 false
Why did the writable flag change without throwing any error.
因为规范允许将数据 属性 上的 writable
标志从 true
更改为 false
即使 属性 的 configurable
属性 是 false
。遗憾的是,规范并没有大篇幅地说明 为什么 事情是这样的,但它通常非常精确地说明了 是什么。 :-)
I want to know what happens In the below two cases:
你最好的选择是 read through the specification's steps for the operation。
1) configurable flag is set to true
在这种情况下,您可以将 writable
从 false
更改为 true
或者从 true
更改为 false
; 属性 是完全可配置的。
2) configurable flag is set to false
规范允许将数据 属性 上的 writable
标志从 true
更改为 false
,但 不是 false
到 true
.
我正在编写 javascript 程序以了解将可配置标志设置为 false 如何影响 属性。程序是:
let user={
name:"heman"
};
Object.defineProperty(user, "name",{
configurable:false
});
try{
Object.defineProperty(user, "name",{
configurable:true
})
}//Throws error
catch(err){console.log(err)}
try{
Object.defineProperty(user, "name",{
enumerable:false
});
} //Throws Error
catch(err){console.log(err)}
try{
Object.defineProperty(user, "name", {
writable:false
});
} //Does'nt Throws Error. But Why?
catch(err){console.log(err)}
在这个程序中,我首先创建了一个名为 user 的对象,user 包含一个 属性 即 name:"heman"。我将名称 属性 的可配置标志设置为 false (configurable:false)。然后我再次尝试将可配置标志更改为 true 它引发了错误。在我尝试将可枚举标志更改为与其默认值相反的 false 后,它再次抛出错误。这次我试图将可写标志更改为 false。标志改变了,没有抛出任何错误。即使将可配置标志设置为 false 为什么 属性 仍然可配置?为什么可写标志发生变化而没有抛出任何错误。我想知道在以下两种情况下会发生什么:
1) 可配置标志设置为真
2) 可配置标志设置为 false
Why did the writable flag change without throwing any error.
因为规范允许将数据 属性 上的 writable
标志从 true
更改为 false
即使 属性 的 configurable
属性 是 false
。遗憾的是,规范并没有大篇幅地说明 为什么 事情是这样的,但它通常非常精确地说明了 是什么。 :-)
I want to know what happens In the below two cases:
你最好的选择是 read through the specification's steps for the operation。
1) configurable flag is set to true
在这种情况下,您可以将 writable
从 false
更改为 true
或者从 true
更改为 false
; 属性 是完全可配置的。
2) configurable flag is set to false
规范允许将数据 属性 上的 writable
标志从 true
更改为 false
,但 不是 false
到 true
.