Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the characteristics of javascript object properties

Shulou Source: shulou.com Published: 2022-06-02 15:47:27 09月24日 Update

This article introduces the relevant knowledge of "what are the characteristics of javascript object properties". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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 attribute console.log (obj.age) / / 12, enumerable: enumerable

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

Let obj = {name: "zhang", age:20, sex: "male"} let newObj = Object.create (obj) newObj.height = 200for (p in newObj) {console.log (p, "- >", newObj [p])}

Output:

Height-> 200

Name-> zhang

Age-> 20

Sex-> male

3. Configurable: configurable

Configurable indicates whether attributes can be deleted through delete

Let obj = {name: "jim"} delete obj.name / / the attribute will not exist console.log (obj.name) / / undefined after deletion

The above three properties in the object are all true by default. If you want to change the default values for these features, you can use the Object.defineProperty () method. DefineProperty receives three parameters, the object, the attribute name to be modified, and the eigenvalue object.

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

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

When writable is set to false, even if the sex property is re-assigned to female, its value is still the original male, and the enumerable and configurable properties of the property can be configured through similar operations.

This is the end of the content of "what are the characteristics of javascript object properties". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Attributes objects features properties accessibility configuration content methods more knowledge output utility learning and then parameters names dilemmas facts situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Redmi NVidia Linux