Web前端必做笔记之一:JavaScript中对象属性的描述,可枚举,可配置,可重写
<script>
const student = {
name:"小明",
age:12,
[Symbol("level")]:"优秀",
[Symbol("level")]:"有钱",
}
const val = Object.getOwnPropertyDescriptor(student,"name");
console.log(val);
Object.defineProperty(student,"gender",{
configurable: true,
enumerable: true,
value: "男",
writable: true
})
console.log(student);
delete student.gender;
</script>
<script>
Object.defineProperty(student,"gender",{
configurable: false,
enumerable: true,
value: "男",
writable: true
})
delete student.gender;
console.log(student);
</script>
<script>
Object.defineProperty(student,"gender",{
configurable: false,
enumerable: true,
value: "男",
writable: true
})
student.gender = "女"
console.log(student);
for(let pro in student){
console.log(pro);
}
</script>
总结: 1. configurable: false, //属性是否可以删除 enumerable: true, //属性是否可枚举, value: “男”, //属性值 writable: true //属性是否可以修改 2.for…in循环,true,可枚举的就能够遍历到,false,不可枚举就遍历不到
|