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 === true); console.assert(xout.enumerable === true); console.assert(xout.configurable === true); // check if a property is enumerable console.assert(Reflect.getOwnPropertyDescriptor({ pp: 4 }, "pp").enumerable === true); // non-exist key console.assert(Reflect.getOwnPropertyDescriptor({ pp: 4 }, "yyyyy") === undefined);
JavaScript. Define Properties and attributes.
- JS: Property Attributes
- JS: Enumerable Property
- JS: Property Descriptor
- JS: Object.create
- JS: Object.defineProperty ❌
- JS: Reflect.defineProperty
- JS: Object.defineProperties
- JS: Object.prototype.propertyIsEnumerable ❌
- JS: Object.getOwnPropertyDescriptor ❌
- JS: Reflect.getOwnPropertyDescriptor
- JS: Object.getOwnPropertyDescriptors