473,597 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById('eleme nt');

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 1462
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.getEle mentById('eleme nt');

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("__lookupSet ter__" in el.style
&& el.style.__look upSetter__("col or")) {

}

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.getEle mentById('eleme nt');

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-ElementCSSInlin eStyle>
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.de mon.co.uk>
Aug 27 '08 #3
On Aug 27, 10:50 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.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.getEle mentById('eleme nt');
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-ElementCSSInlin eS...>

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$8 300d...@news.de mon.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.getEle mentById('eleme nt');
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
2530
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 function or a xmlFilename member to get the filename they are from. Right now I have the following code:
2
3975
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 example: A : function () {} A.prototype = {
13
1572
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(){ return Math.PI * this.r * this.r; } var c = new Circle(0,0,1);
0
1531
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 I wanted to do is to add a ToolTip object on the starting form, and pass this ToolTip object to the user controls. In each user control I created a property ToolTip with only a setter which accepts the passed ToolTip.
1
1022
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 variable (targetId) in to the usercontrol (IntergySite.aspx) by calling its setter method. Currently, I am using if-then-else and hardcoded the User Control Object to do casting and call the setter method. Question: Is there any way I could load,...
37
2569
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
8456
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 code: public override string Text { get { return base.Text;
3
2025
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 handle/reference/ pointer to an attribute. I know I could make my own class to do this (using the __dict__ of the object and the attribute name), but I would like to use something standard (at least a protocol) if it exists. All that is needed in the protocol...
4
1550
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 noticed that when returning a new TPastePart, created and filled by the web service, that if the "PastePartID" and "PastePart" properties do not have "set" accessor methods, their respective data is not returned, even though the object has contains...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5431
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.