/    Sign up×
Community /Pin to ProfileBookmark

How to check if property is getter only?

Hey,

I’m wondering whether there is a (fast/easy) way to determine if an objects property is a read-only property (a getter).

My script is going to try and assign values to properties of objects, usually HTMLElements.
But it’ll come across some properties that are getters, like this:

[CODE]
var myEl = document.getElementById(“someDiv”);

var propName = “offsetWidth”;

myEl[propName] = someValue;
[/CODE]

Don’t worry about this example, it’s far from my actual implementation.

So far I’ve considered using a try statement around the assignment so I can ignore the error, but I’m not certain about the speed of a try statement and speed is an important factor (lots of looping going on in there).

So, does anyone know of an easy way to determine if a property is read-only?

Thanks in advance.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienNov 07.2007 — Using [B]try-catch[/B] blocks could be a way, but they are not as efficient as just knowing which properties cannot be set. It would be best to create an array of property names that cannot have their value set, and then test the property name passed to the function against this array of invalid property names.
/**
* @class window
*
* @function inArray
* Tests to see if something is in an array.
*
* @param needle (variable, required)
* The item to find.
*
* @param haystack (array, required)
* The array to search.
*
* @return boolean
* True if it is found, false otherwise.
*/
function inArray(needle, haystack) {
for (var i=0, end=haystack.length; i < end; i++) {
if (haystack[i] === needle) {
return true;
}
}
return false;
}

You'll need the function above. Then in your function that sets a property value:
var gettersOnly = ['innerHTML','offsetHeight','offsetWidth'];

if (!inArray(propName, gettersOnly)) {
myEl[propName] = someValue;
}
Copy linkTweet thisAlerts:
@Angry_Black_ManNov 07.2007 — ive been looking at this for hours. other than using the error invoked by trying to change that property, there is nothing i can find that would directly or indirectly tell you that the property was read only.

as far as the try catch method, that seemed to be the only viable solution. i was very dissapointed.
Copy linkTweet thisAlerts:
@NietechtauthorNov 07.2007 — Thanks for the answers.

An array with the "invalid" properties did cross my mind at first, but I dismissed it because the array would be have to be bloated for cross-browser compatibility.

But without any alternative to the try statement and upon your advice, toicontien, I tried to compare the speeds of the 2 techniques anyway.

The array won, very decisively. Even with a very large array of foo elements leading the actual invalid properties, looping the array was still several times faster than using the try statement.

I guess the try statement is a lot slower than I thought.

I also considered another advantage to the array: it lets me add properties that are not read-only but that I wouldn't want to touch anyway, like innerHTML and outerHTML.

Collecting all the read-only properties of HTMLElements will be a pain though, seeing how all browsers have their own list...

Thanks for the advice.
×

Success!

Help @Nietecht spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...