Jon Doe
Jon Doe

Reputation: 9

why to use function to define a property?

i read about defineProperty().

but i found this notation in a program i don't understand:

myObject.defineProperty("something", "propertyNameExample", function()
{
// function body here
})

what i do not understand is that function() {...} notation.

what role does it play for "propertyNameExample"?

please explain me. thank you

Upvotes: 0

Views: 42

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

Nothing (or technically not sure), this is not a native method for defining a property in Javsacript.

There are two things wrong here

  • It should be Object.defineProperty not myObject.defineProperty
  • myObject.defineProperty will be a user defined function not a native function.

As per documentation, third argument is a descriptor object not a function.

Even if you pass a function as the parameter, it doesn't affect the functionality in any way, whether you are setting a property or getting a property.

Upvotes: 1

Related Questions