Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Create property Getter and Setter ?

  1. #1
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create property Getter and Setter ?

    please tell me how to create the property Getter and Setter ?

  2. #2
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by svincoll4
    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

  4. #4
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

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

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.
    Last edited by svincoll4; 06-25-2005 at 01:06 AM.

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    PLEASE: Include the URL to your problematic webpage that you want help with.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I guess you want to apply OOP

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

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

    done

  9. #9
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by svincoll4
    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:

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


    Quote Originally Posted by dd2me
    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

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Does JavaScript support OOP?
    If so, you can use:

    Code:
    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
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •