Doc
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
writable
false
var test = {"key": "value"};
Object.defineProperty(test, 'key2', {
value: 'value2',
writable: false
})
console.log(test) // {key: "value", key2: "value2"}
test.key = 'pre-' + test.key;
test.key2 = 'pre-' + test.key2;
console.log(test) // {key: "pre-value", key2: "value2"}
true
var test = {"key": "value"};
Object.defineProperty(test, 'key2', {
value: 'value2',
writable: true
})
console.log(test) // {key: "value", key2: "value2"}
test.key = 'pre-' + test.key;
test.key2 = 'pre-' + test.key2;
console.log(test) // key: "pre-value", key2: "pre-value2"}
configurable
delete
false
var test = {"key": "value"};
Object.defineProperty(test, 'key2', {
value: 'value2',
configurable: false
})
console.log(test) // {key: "value", key2: "value2"}
delete test.key
delete test.key2
console.log(test) // {key2: "value2"}
true
var test = {"key": "value"};
Object.defineProperty(test, 'key2', {
value: 'value2',
configurable: true
})
console.log(test) // {key: "value", key2: "value2"}
delete test.key
delete test.key2
console.log(test) // {}
change descriptor
false
var test = {};
Object.defineProperty(test, 'key', {
get: function() {
return 'value'
},
configurable: false
})
console.log(test.key) // value
Object.defineProperty(test, 'key', {
get: function() {
return 'value2'
},
configurable: false
}) // Uncaught TypeError: Cannot redefine property: key
true
var test = {};
Object.defineProperty(test, 'key', {
get: function() {
return 'value'
},
configurable: true
})
console.log(test.key) // value
Object.defineProperty(test, 'key', {
get: function() {
return 'value2'
},
configurable: false
})
console.log(test.key) // value2
enumerable
false
var test = {"key": "value"};
Object.defineProperty(test, 'key2', {
value: 'value2',
enumerable: false
})
console.log(Object.keys(test)) // ["key"]
true
var test = {"key": "value"};
Object.defineProperty(test, 'key2', {
value: 'value2',
enumerable: true
})
console.log(Object.keys(test)) // ["key", "key2"]