JS: Reflect.getOwnPropertyDescriptor
(new in ECMAScript 2015)
Reflect.getOwnPropertyDescriptor(obj, key)-
- Return the Property Attributes in the form of Property Descriptor.
- If the property doesn't exist, returns
undefined. - If obj is not object, throw a TypeError exception.
🟢 tip: This is a improved version of Object.getOwnPropertyDescriptor because it fixed the problem of auto convert arg. It's also a replacement for Object.prototype.propertyIsEnumerable because that won't work if object has the same property name.
const jj = { pp: 4 }; const xout = Reflect.getOwnPropertyDescriptor(jj, "pp"); // { value: 4, writable: true, enumerable: true, configurable: true } console.assert(Object.keys(xout).length === 4); console.assert(xout.value === 4); console.assert(xout.writable); console.assert(xout.enumerable); console.assert(xout.configurable); // check if a property is enumerable console.assert(Reflect.getOwnPropertyDescriptor({ pp: 4 }, "pp").enumerable ); // non-exist key console.assert(Reflect.getOwnPropertyDescriptor({ pp: 4 }, "yyyyy") === undefined);