469,349 Members | 1,490 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

How to check if an object has defined setter?

Hi!

Ok, so I have small problem and can't find solution to it.. Is there
any way to check if an object has defined setter? In other words - if
it's value can be changed?

Small example:

var el = document.getElementById('element');

el.style = "smth";

Browser will throw exception, I can use try..catch but is there way to
just write a function like hasSetter() ?

Thanks fo help!
Aug 27 '08 #1
3 1230
Piotr K wrote:
Hi!

Ok, so I have small problem and can't find solution to it.. Is there
any way to check if an object has defined setter? In other words - if
it's value can be changed?

Small example:

var el = document.getElementById('element');

el.style = "smth";

Browser will throw exception, I can use try..catch but is there way to
just write a function like hasSetter() ?

To look up a setter in environments that support that:
if("__lookupSetter__" in el.style
&& el.style.__lookupSetter__("color")) {

}

But that would not tell you if the value can be changed, or what
acceptable values are. You'd just have to know it. For example, given el
is an HTMLElement:-

el.style.color = null;

Would be a mistake.

If there were edge cases that couldn't be eliminated, you could use
try/catch. But that would add clutter and slow the program down.
Garrett

Aug 27 '08 #2
Piotr K wrote:
Ok, so I have small problem and can't find solution to it.. Is there
any way to check if an object has defined setter? In other words - if
it's value can be changed?
No, but you can test whether the value changed after you tried.
Small example:

var el = document.getElementById('element');

el.style = "smth";

Browser will throw exception,
BAD. The `style' property of element objects is implemented as a reference
to an object, and its interface specifies it as being read-only.
I can use try..catch but is there way to just write a function like
hasSetter() ?
No. Adhering to standards stands the best chance of not throwing an
exception here.

<http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-ElementCSSInlineStyle>
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 27 '08 #3
On Aug 27, 10:50 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Piotr K wrote:
Ok, so I have small problem and can't find solution to it.. Is there
any way to check if an object has defined setter? In other words - if
it's value can be changed?

No, but you can test whether the value changed after you tried.
Small example:
var el = document.getElementById('element');
el.style = "smth";
Browser will throw exception,

BAD. The `style' property of element objects is implemented as a reference
to an object, and its interface specifies it as being read-only.
I can use try..catch but is there way to just write a function like
hasSetter() ?

No. Adhering to standards stands the best chance of not throwing an
exception here.

<http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-ElementCSSInlineS...>

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300d...@news.demon.co.uk>
Do what ever you need to try and do in a try and catch statement ...
e.g

var worked = false;
try
{
var el = document.getElementById('element');
el.style = "smth";
worked=true;
}catch(e)
{
//in here you can alert(e.message); or set an internal error message
}
if(worked==true)
{
//worked
}else{
//didnt work
}
Aug 28 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Nicolas Fleury | last post: by
2 posts views Thread by Wei Wang | last post: by
13 posts views Thread by Nathan White | last post: by
reply views Thread by Yves Royer | last post: by
3 posts views Thread by Eric Mahurin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.