PDA

View Full Version : Create property Getter and Setter ?



svincoll4
06-22-2005, 05:03 AM
please tell me how to create the property Getter and Setter ?

cr3ative
06-22-2005, 07:50 AM
I believe it is as follows:

As text:
var Getter="";
var Setter="";

As numerical:
var Getter=0;
var Setter=0;

Although I have been known to be wrong.
cr3ative

mwinter
06-22-2005, 10:26 AM
please tell me how to create the property Getter and Setter ?Care to be a little more specific? The word 'property' refers to a member of an object, so where is this object and how is it defined? If you're actually referring to variables, then cr3ative has just demonstrated that.

Mike

svincoll4
06-23-2005, 02:18 AM
mwinter right, i want to create a property which refers to a member of an object, it's not variables. Thanks cr3ative.

jscheuer1
06-24-2005, 05:38 AM
Yeah, I'm confused too. Rather than explaining it in such general terms. Tell us exactly what you want to do and what you want to do it to. Be specific, show us some coding attempts and explain what you wanted to have happen that didn't. For some reason this thread is reminding me of the getAttribute('attribute_name') and setAttribute('attribute_name','attribute_value') methods. Am I even warm?

svincoll4
06-25-2005, 01:02 AM
sorry all, i say English not good.
i have an object: o_user. I want to create an property such then:
o_user.u_bgcolor='color' ;
then, the background color of document will changed.
Accept all ideas. Please help.

jscheuer1
06-25-2005, 02:36 AM
We need more than that. How did the object get the name o_user? Is it the id of an element? Is it a variable that was assigned when an element was created? In order to really understand, it would be good if you:

.

dd2me
06-29-2005, 09:06 AM
I guess you want to apply OOP

yourfunc.prototype.getYourVar() {
return this.yourVar;
}

yourfunc.prototype.setYourVar(val) {
this.yourVar = val;
}

done

mwinter
06-29-2005, 03:49 PM
i have an object: o_user. I want to create an property such then:
o_user.u_bgcolor='color' ;
then, the background color of document will changed.Without making assumptions as to what o_user is, I'd have to say that's not possible.

Host objects and their properties do often cause changes within the document, but that's because they are special and the browser can monitor them. However, native objects (ones that you create yourself) cannot be watched. You would have to use functions:


o_user.setBackground = function(colour) {
var body = document.body,
style;

if(body && (style = body.style)) {
style.backgroundColor = colour;
}
};If o_user is a host object (such as a reference to a HTML element), then as John said we need more information. Preferably an example of what you've tried, and failed, to do.



yourfunc.prototype.getYourVar() {
return this.yourVar;
}That would just be a syntax error - a function call immediately followed by a block statement. You'd need to assign a function object to getYourVar similar to how I have above. However, in this simple example, there's no reason why one couldn't assign to the property directly.

Mike

Twey
06-29-2005, 07:03 PM
Does JavaScript support OOP?
If so, you can use:


class MyClass {
private int green;

public int getGreen() {
return green;
}

public void setGreen(newgreen) {
green = newgreen;
}

}

This is how I would do it in Java. I have no idea if this would work with JavaScript.

As a matter of interest, C# can actually integrate getters and setters into the variable itself, providing better control over variable access. I love this :p

flashgod
03-18-2007, 05:37 AM
o_user = new Object;
o_user = { get u_bgcolor() { return document.body.bgColor; }, set u_bgcolor(x) { document.body.bgColor = x; } };

something like this should do it :)