Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of javascript object properties

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report