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

How to use the Object.getOwnPropertyNames () method in JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to use the Object.getOwnPropertyNames () method in JavaScript". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use the Object.getOwnPropertyNames () method in JavaScript".

Object.getOwnPropertyNames

The Object.getOwnPropertyNames () method also takes an object as a parameter and returns an array of properties and methods that match within that object.

You will ask here, what's the difference between the two? Object.getOwnPropertyNames () can return all properties, while Object.keys () can only return enumerable properties, eh? At this point, people are wondering, but what is the enumeration attribute? Don't worry, let me explain what an enumerable attribute is.

Enumerable attribute

Enumerable or non-enumerable attributes are distinguished by the enumerable flag enumerable inside the object. By default, after we obj.name = "cxy" add a property to the object, the enumerable flag enumerable is ture, but when its value is false, it cannot be enumerated. When we perform for,Object.keys () and JSON.stringify () on the object, the non-enumerable attribute cannot be found. We can understand that non-enumerable attributes are invisible.

Now let's take the above Object.getOwnPropertyNames and Object.keys () for practical examples. We use defineProperty to add the age property to the object, because this method can set the enumeration flag, which is set to false, and you can see the following two different results

Var stuObj = {name: "cxy"} Object.defineProperty (stuObj, 'age', {value: "18", enumerable: false}); console.log (Object.keys (stuObj)) console.log (Object.getOwnPropertyNames (stuObj))

The above is all the content of the article "how to use the Object.getOwnPropertyNames () method in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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