Object.Getownpropertynames VS Object.Keys

Object.getOwnPropertyNames vs Object.keys

There is a little difference. Object.getOwnPropertyNames(a) returns all own properties of the object a. Object.keys(a) returns all enumerable own properties. 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.

It's easy to test:

var a = {};
Object.defineProperties(a, {
one: {enumerable: true, value: 1},
two: {enumerable: false, value: 2},
});
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:

a.test = 21;

then such property becomes an enumerable automatically and both methods produce the same array.

Function not listed under Object.keys or Object.getOwnPropertyNames but can be invoked

Object.e must be defined on the object's prototype. Like this:

var test = function() {}
test.prototype = { e: function() { return 'e'; } }
var obj = new test();
Object.keys(obj) // returns []
obj.e() // returns 'e'

A way of getting the prototypes keys is simply getting the prototype and the use the Object.keys() function:

Object.keys(Object.getPrototypeOf(obj))

This will however not give you keys of the prototypes prototype.

What is the difference between Reflect.ownKeys(obj) and Object.keys(obj)?

Object.keys() returns an array of strings, which are the object's own enumerable properties.

Reflect.ownKeys(obj) returns the equivalent of:

Object.getOwnPropertyNames(target).
concat(Object.getOwnPropertySymbols(target))

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

The Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object.

var testObject = {};
Object.defineProperty(testObject, 'myMethod', {
value: function () {
alert("Non enumerable property");
},
enumerable: false
});

//does not print myMethod since it is defined to be non-enumerable
console.log(Object.keys(testObject));

//prints myMethod irrespective of it being enumerable or not.
console.log(Reflect.ownKeys(testObject));

Why does Object.keys() and Object.getOwnPropertyNames() produce different output when called on a Proxy object with ownKeys handler?

The difference between Object.keys() and Object.getOwnPropertyNames() is that Object.keys() will invoke [[GetOwnProperty]] on the object, and only add the property to the result list if the returned property descriptor is enumerable. Since the object doesn't have a such a property, [[GetOwnProperty]] will return undefined and the property (name) is ignored.

You can overwrite/implement [[GetOwnProperty]] in the proxy by implementing getOwnPropertyDescriptor:

const p = new Proxy({}, {  ownKeys(target) {    return ['a', 'b'];  },  getOwnPropertyDescriptor(k) {    return {      enumerable: true,      configurable: true,    };  }});
console.log(Object.getOwnPropertyNames(p)); // ['a', 'b']console.log(Object.keys(p)); // ['a', 'b']

Why doesn't Object.getOwnPropertyNames() list all properties and methods?

Cause the properties you list (indexOf, ...) are not part of the string object itself, but are rather part of its prototype:

Object.getOwnPropertyNames(
Object.getPrototypeOf("str")
)

After Object.create: properties are there, but Object.keys / getOwnPropertyNames = []?

Object.create has two arguments. The first one is the prototype of the created object, and a second optional parameter is an object of property descriptors.

Object.create(proto[, propertiesObject])

If you create the object passing the properties object as the first argument,

let obj = Object.create({x: 1, y: 2});

this one will become the prototype of your new object.
As Object.keys() returns an array of only its own enumerable properties, the ones you passed when constructing it won't be listed.

To create the object the way you intended, yo can use Object.assign:

let obj = Object.assign({}, {x: 1, y: 2});

Inconsistency between console.log, Object.keys and Object.getOwnPropertyNames

docs is a list of Mongoose document objects. They don't have fields available for enumeration, there are accessors defined that make them available as doc.fieldName.

There are document toObject and toJSON methods to convert document object to plain object when needed.

The actual problem here is that since document object aren't needed, they shouldn't be queried. Plain objects can be retrieved with lean.

    Kitten.find({}).lean().exec((err, docs) => {
for (var i = 0; i < docs.length; ++i) {
const doc = docs[i]
...
}
});


Related Topics



Leave a reply



Submit