JS: Reflect.getOwnPropertyDescriptor

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

Reflect.getOwnPropertyDescriptor(obj, key)

🟢 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.