jskatas.org Continuously Learn JavaScript. Your Way.

Reflect: Reflect.defineProperty()

Defines a property on a given object.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

Reflect.defineProperty() is like Object.defineProperty() but returns a Boolean.

the function itself

  • is static on the Reflect object
  • is of type function

the 1st parameter is the object on which to define a property

  • fails if it is not an object
  • accepts an object
  • accepts an instance (of a class)

2nd parameter is the name of the property to be defined on the object (normally a string)

  • works with a normal string
  • a number gets converted into a string
  • undefined also gets converted into a string (watch out!)
  • it can be a symbol

the value is part of the 3rd parameter, given as a property in an object {value: ...}

  • contains the initial value of the property, as an object in the property value
  • can be of any type (even itself)

the return value of the function indicates wether the property was defined successfully

returns true

  • when the property was created (which requires the 3rd parameter too!!!)
  • no matter what the value of the property is (just the 3rd param has to exist as {})

returns false

  • when a non-configurable property wants to be changed to configurable=true
  • when the object we want to add a property to is frozen


The specification for this function.
The MDN docs for this function.
Announcement of this kata on twitter.