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

MySQL scheduled database backup operation example

This article describes the example of MySQL sched...

Creating private members in JavaScript

Table of contents 1. Use closures 2. Use ES6 clas...

Configure Mysql master-slave service implementation example

Configure Mysql master-slave service implementati...

Where is mysql data stored?

MySQL database storage location: 1. If MySQL uses...

Graphical explanation of the function call of proto file in Vue

1. Compile proto Create a new proto folder under ...

Some details about semicolons in JavaScript

Preface Semicolons in JavaScript are optional, an...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

First experience of creating text with javascript Three.js

Table of contents Effect Start creating text Firs...

MySQL 8.0.11 installation summary tutorial diagram

Installation environment: CAT /etc/os-release Vie...

Detailed explanation of the solution to image deformation under flex layout

Flex layout is a commonly used layout method nowa...

Teach you to quickly build a web cluster project based on nginx

Table of contents 1. Project Environment 2. Proje...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

Introduction to JavaScript conditional access attributes and arrow functions

Table of contents 1. Conditional access attribute...

jQuery implements the mouse drag image function

This example uses jQuery to implement a mouse dra...