Results 1 to 5 of 5

Thread: Objects: Get/Set

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Objects: Get/Set

    Could someone explain to me the purpose of "set" and "get"?

    Code:
    o = {
      a:7,
      get b() { return this.a+1; },
      set c(x) { this.a = x/2; }
    };
    
    alert(o.a);	// 7
    alert(o.b);	// 8
    o.c = 8;
    alert(o.a);	// 4
    alert(o.b);	// 5
    To me they seem like methods that are not methods, kind of like a mix between variables and methods.

    But the way I see people normally code (or with slight variations, such as prototypes and such), the object would be coded like:

    Code:
    o = {
      a:7,
      b: function() { return this.a+1; },
      c: function(x) { this.a = x/2; }
    };
    
    
    alert(o.a);	// 7
    alert(o.b());	// 8
    o.c(8);
    alert(o.a);	// 4
    alert(o.b());	// 5

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

    Default

    Yup, you're quite right, there's no real point to them. They're just syntactic sugar: they can be used to make things look nicer.
    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!

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

    Default

    Quote Originally Posted by Twey View Post
    Yup, you're quite right, there's no real point to them. They're just syntactic sugar: they can be used to make things look nicer.
    As I recall, it is a Gecko extension to ECMAScript, so it's pointless for the Web anyway.
    Mike

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

    Default

    Yes, although it's included in Mozilla's ECMAScript 4 proposal. I think this proposal may have been rejected, however.
    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!

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

    Default

    Quote Originally Posted by Twey View Post
    Yes, although it's included in Mozilla's ECMAScript 4 proposal. I think this proposal may have been rejected, however.
    It would still be pointless for the Web until about five years after the proposal is accepted and first implemented in all "major" browsers.
    Mike

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
  •