+ 1
Difference between these methods?
Any difference in these three object.keys() for in loop object.getOwropertyNames()
3 Respuestas
+ 2
yeah there is one difference i know
object.keys() will return an array of all keys which are enumerable ( means countable or numerable ).
object.getOwnpropertyNames() will return an array of all the key, does not matter if they are enumerable or not . it will return every property.
similarly
for in loop will iterate over only those keys which are enumerable.
for example
var objName = {
prop1: { enumerable: true, value: 100 },
prop2: { enumerable: false, value: 9999 },
prop3: 555 // by default all property enumerable value is set to true
}
then
Object.keys(objName) // [ "prop1", "prop3" ]
Object.getOwnpropertyName(a) // [ "prop1", "prop2", "prop3" ]
for in loop will iterate over "prop1" and "prop3"
only not "prop2"
0
Ty. You mean by default they all are same if we didnt specify whether its enumerable(which i think most people do)
0
UMAR FAROOQ
yes👍