HighDots Forums  

How to check if an object has defined setter?

Javascript JavaScript language (comp.lang.javascript)


Discuss How to check if an object has defined setter? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Piotr K
 
Posts: n/a

Default How to check if an object has defined setter? - 08-27-2008 , 03:43 PM






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!

Reply With Quote
  #2  
Old   
dhtml
 
Posts: n/a

Default Re: How to check if an object has defined setter? - 08-27-2008 , 05:23 PM






Piotr K wrote:
Quote:
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



Reply With Quote
  #3  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: How to check if an object has defined setter? - 08-27-2008 , 05:50 PM



Piotr K wrote:
Quote:
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.

Quote:
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.

Quote:
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, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>


Reply With Quote
  #4  
Old   
Laser Lips
 
Posts: n/a

Default Re: How to check if an object has defined setter? - 08-28-2008 , 06:08 AM



On Aug 27, 10:50 pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
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-Sty...mentCSSInlineS...

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... (AT) news (DOT) 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
}




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.