473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1454
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nicolas Fleury | last post by:
Hi everyone, I'm wondering what is the easiest/cleanest way to look for existence of available methods/members. For example, in parsing a xml file, created objects can define a setXmlFilename...
2
by: Wei Wang | last post by:
Greetings, I find the JavaScript's Object.prototype and getter/setter mechanism very nice. However, I need some help with extending an object with getters/setters in the derived class. For...
13
by: Nathan White | last post by:
I was curious if I can make an object method look and act like an object property. example: function Circle(x,y,r){ this.x = x; this.y = y; this.r = r; } Circle.prototype.area = function(){...
0
by: Yves Royer | last post by:
Hi, I'm currently writing an application (in VS 2005 bèta 2) and made some own user controls. In each user control I added a ToolTip object so i can set some tooltips on checkboxes etc. What...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
4
by: UJ | last post by:
I have a text object (see my previous post) that I've added some functionality to. But I don't want the user to be able to change the text without calling my routines. So I added the following...
3
by: Eric Mahurin | last post by:
Is there a standard way to get a descriptor object for an arbitrary object attribute - independent of whether it uses the descriptor/ property protocol or not. I want some kind of...
4
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.