Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

javascript - Object.getOwnPropertyNames与Object.keys(Object.getOwnPropertyNames vs Object.keys)

What's the difference between Object.getOwnPropertyNames and Object.keys in javascript?(JavaScript中的Object.getOwnPropertyNamesObject.keys什么区别?)

Also some examples would be appreciated.(还可以理解一些示例。)   ask by kamilkp translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a little difference.(有一点区别。)

Object.getOwnPropertyNames(a) returns all own properties of the object a .(Object.getOwnPropertyNames(a)返回对象a 所有自身属性。) Object.keys(a) returns all enumerable own properties.(Object.keys(a)返回所有可枚举的自身属性。) It means that if you define your object properties without making some of them enumerable: false these two methods will give you the same result.(这意味着,如果在定义对象属性时未使它们中的一些成为enumerable: false这两种方法将为您提供相同的结果。) It's easy to test:(很容易测试:) var a = {}; Object.defineProperties(a, { one: {enumerable: true, value: 'one'}, two: {enumerable: false, value: 'two'}, }); Object.keys(a); // ["one"] Object.getOwnPropertyNames(a); // ["one", "two"] If you define a property without providing property attributes descriptor (meaning you don't use Object.defineProperties ), for example:(如果在不提供属性属性描述符的情况下定义属性(意味着您不使用Object.defineProperties ),例如:) a.test = 21; then such property becomes an enumerable automatically and both methods produce the same array.(然后,该属性将自动成为可枚举的,并且这两种方法都将产生相同的数组。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...