Standard built-ins

Object.defineProperty()

Try the free JavaScript compiler

Defines or redefines an own property using descriptor attributes. Omitted flags on a new data property default false; incompatible changes throw TypeError.

Formal Syntax & Structure

javascript syntaxjavascript
Object.defineProperty(obj, key, descriptor)

Parameters & Attributes

NameTypeDescription
objObjectTarget object.
keyString | SymbolProperty key.
descriptorObjectProperty descriptor with valid data or accessor fields.

Return Value

Object

Same target.

Working Example

Example Snippetjavascript
const o={};Object.defineProperty(o,"n",{value:2});console.log(o.n,Object.keys(o).length);
JavaScript Reference