kevinius
kevinius

Reputation: 4618

Is defineProperty method inherited from Object?

If all objects inherit from Object then why can't i use the defineProperty method of Object in this way?

var car = {name:"honda"}

car.defineProperty(car, "jow", {value:"jow"})

alert(car.jow)

Thx for your insight,

Upvotes: 0

Views: 124

Answers (1)

pax162
pax162

Reputation: 4735

Because defineProperty is not a property of Object's prototype, it is a property of Object itself.

So to use it, do this: (from documentation, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)

var o = {}; // Creates a new object

// Example of an object property added with defineProperty with a data property descriptor
Object.defineProperty(o, "a", {value : 37,
                               writable : true,
                               enumerable : true,
                               configurable : true});
// 'a' property exists in the o object and its value is 37

Also, here's a good link explaining protos and inheritance: http://msdn.microsoft.com/en-us/magazine/ff852808.aspx

Upvotes: 1

Related Questions