Is it possible, given an object and property name to determine if that property is defined using either a getter or setter, or is it completely transparent? I only want to define a getter/setter if there is not already one defined on the property.
I need it to work in WebKit/Firefox.
Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class. A setter updates the value of a variable, while a getter reads the value of a variable.
JavaScript Accessors (Getters and Setters)ECMAScript 5 (ES5 2009) introduced Getter and Setters. Getters and setters allow you to define Object Accessors (Computed Properties).
Getters and setters allow us to define Object Accessors. The difference between them is that the former is used to get the property from the object whereas the latter is used to set a property in an object.
What are Getters and Setters? Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class. Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.
I think you're looking for getOwnPropertyDescriptor
?
You can use Object.getOwnPropertyDescriptor(obj, prop)
For example:
var obj = { first: 1 } obj.__defineGetter__('second', function() { return 2; }); // get descriptors var descriptor1 = Object.getOwnPropertyDescriptor(obj, 'first'); var descriptor2 = Object.getOwnPropertyDescriptor(obj, 'second'); // check if it's a getter descriptor2.get // returns function () { return 2; } descriptor1.get // returns undefined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With