var user = { name:"angdh", age:11, }; console.log( JSON.stringify(Object.getOwnPropertyDescriptors(user),null,2) );
{ "name": { "value": "angdh", "writable": true, "enumerable": true, "configurable": true }, "age": { "value": 11, "writable": true, "enumerable": true, "configurable": true } }
var user = { name:"angdh", age:11, }; Object.defineProperty(user,"name",{ value:"hhhh", writable:false }); console.log(user.name); //hhhh user.name = "fffff"; console.log(user.name); //hhhh
Object.defineProperty(user,"name",{
value:"hhhh",
writable:false,
enumerable:false, //不容许遍历
configurable: false //不容许删除 ,不容许重新配置
});
console.log(Object.keys(user)); // ["age"]