Three properties of javascript objects

Three properties of javascript objects

Object characteristics:

1. writable: writable

writable indicates whether the value of the property can be set

let obj = {age:10}
obj.age = 1 // Reassign the property console.log(obj.age) //1

2. enumerable: enumerable

The enumerable attribute refers to whether the property name can be returned in for/in loop. By default, both owned and inherited properties can be enumerated.

let obj = {name:"zhang", age:20, sex:"male"}

let newObj = Object.create(obj)
newObj.height = 200

for(p in newObj){
    console.log(p,"->", newObj[p])
}

Output:

height -> 200
name -> zhang
age -> 20
sex -> male

3. Configurable: Configurable

configurable indicates whether the attribute can be deleted through delete

let obj = {name:"jim"}

delete obj.name // After deletion, the property will no longer exist console.log(obj.name) //undefined

The above three properties of the object's own attributes are true by default. If you want to modify the default values ​​of these characteristics, you can use Object.defineProperty() method. defineProperty receives three parameters: object, property name to be modified, and feature value object.

For example, if you want to set the writable property of the sex property to false, you can use the defineProperty() method to do this.

let obj = {name:"zhang", age:20, sex:"male"}
Object.defineProperty(obj, "sex", {writable:false})
obj.sex = "female"
console.log(obj.sex) // Male

After setting writable to false , even if the sex attribute is reassigned to female, its value is still male. Similar operations can be used to configure the enumerable and configurable characteristics of attributes.

This is the end of this article about the three property characteristics of javascript objects. For more relevant content on javascript object property characteristics, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the idea of ​​implementing dynamic columns in angularjs loop object properties
  • JavaScript removes unnecessary properties of an object
  • When the springboot post interface accepts json, when it is converted to an object, the properties are all null.
  • Several ways to easily traverse object properties in JS
  • How to delete a property of an object in JavaScript
  • Use of hasOwnProperty method of js attribute object
  • The JS hasOwnProperty() method detects whether a property is an object's own property.
  • Parsing javascript Date object properties and methods through examples
  • Detailed explanation of dynamic addition, deletion, modification and query of properties when converting Java objects to JSON
  • When converting an object to json, java jackson ignores a property operation of the sub-object

<<:  Hide div in HTML Hide table TABLE or DIV content css style

>>:  Design: A willful designer

Recommend

Detailed explanation of virtual DOM and diff algorithm in react

The role of virtual DOM First of all, we need to ...

MySql quick insert tens of millions of large data examples

In the field of data analysis, database is our go...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Solution to forgetting MySQL root password in MACOS

MySQL is a relational database management system ...

How to migrate the data directory in Docker

Table of contents View Disk Usage Disk Cleanup (D...

Steps to install MySQL 5.7 in binary mode and optimize the system under Linux

This article mainly introduces the installation/st...

Baidu Input Method opens API, claims it can be ported and used at will

The relevant person in charge of Baidu Input Metho...

Which scenarios in JavaScript cannot use arrow functions

Table of contents 1. Define object methods 2. Def...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...