By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

functions that hide as properties

P: n/a
I want a property. But I dont want it to be a static string. Below is
make belive javascript code of what I want.

this.prop1 = function (input)
{if (input == "none because we are doing output")
{alert("You are retriving prop1");
return this.prop2;};
else
{alert("You are setting prop1 to" + input);
this.prop2 = input;};
};

or

this.prop1.onread.nodeValue = "alert('You are retriving prop1');
return this.prop2;";

this.prop1.onset.nodeValue = "function (setvalue) {
alert("You are setting prop1 to" + setvalue);
this.prop2 = setvalue;};";

I could easily impliment it as a bunch of functions, but then the code
would really be hard to understand and deal with. I simply have to make
them look and feel like propertys, but infact be something else. Its
advanced OOP I am trying to do here (which I am not good at). I
basically need a property, that when I set it, a function will do some
logic and eventually pass on what the property was set to another
variable in DOM. And when I read the property, a function will do some
logic and figure out what to value/variable to return, and return it.

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 3 Jan 2005 18:58:04 -0800, <bu****@hotmail.com> wrote:
I want a property. But I dont want it to be a static string. Below is
make belive javascript code of what I want.

this.prop1 = function (input)
{if (input == "none because we are doing output")
{alert("You are retriving prop1");
return this.prop2;};
else
{alert("You are setting prop1 to" + input);
this.prop2 = input;};
};


The typeof operator can tell you what type a variable equates to,
including if it's undefined.

this.myProperty = function(arg) {
if('undefined' == typeof arg) {
/* Act as output */
} else {
/* Act as input */
}
};

If the "property" is only supposed to receive a certain type, you could
make the typeof set more specific. The strings that typeof will return
are: 'number', 'boolean', 'string', 'undefined', 'object', and 'function'.
Be aware that some host objects will yield 'function' not 'object' on some
hosts, and null will yield 'object'.

For example, if the "property" should only contain numbers:

this.myProperty = function(arg) {
if('undefined' == typeof arg) {
/* Act as output */
} else if('number' == typeof arg) {
/* Act as input */
} else {
/* Error (ignore as you see fit) */
}
};

You could save the value obtained from the typeof operator, rather than
applying it twice.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.