In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to choose Reflect.ownKeys () and Object.keys ()". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to choose Reflect.ownKeys () and Object.keys ()".
Consider a question: how to traverse the Keys of Object in JavaScript and encounter the two methods of Reflect.ownKeys (obj) and Object.keys (obj), what is the difference between them?
Let's first look at an example:
Const obj = {a: 5, b: 5}; console.log (Reflect.ownKeys (obj)); console.log (Object.keys (obj)); / / Result / / ['asides,' b'] / / ['asides,' b']
It doesn't seem to be any different. Go through the documents of the two methods and learn:
Object.keys (obj) returns an array of enumerable properties of a given object in the same order that property names are returned when traversing the object in a normal loop.
The result returned by Reflect.ownKeys (obj) is equivalent to:
Object.getOwnPropertyNames (target) .concat (Object.getOwnPropertySymbols (target))
The Object.getOwnPropertyNames () method returns an array of property names of all its own properties of the specified object, including properties that cannot be enumerated but do not include symbol values as names.
The Object.getOwnPropertySymbols () method returns an array of all the Symbol properties of a given object itself.
Reflect.ownKeys (obj) equals to returning all the properties of a given object as an array.
Let's see the difference through the following code:
Var testObject = {[Symbol ('1')]: 1, a: 2}; Object.defineProperty (testObject, 'myMethod', {value: function () {alert ("Non enumerable property");}, enumerable: false}); console.log (Object.keys (testObject)); / / [' a'] console.log (Reflect.ownKeys (testObject); / / ['asides,' myMethod', Symbol (1)]
Summary
Object.keys (obj): the result is all enumerable key on object
Reflect.ownKeys (obj): the result is all key.
Thank you for your reading, the above is the content of "how to choose Reflect.ownKeys () and Object.keys ()". After the study of this article, I believe you have a deeper understanding of how to choose Reflect.ownKeys () and Object.keys (), and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.