Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    syntax question - dynamic property?

    I came across this code and have a question about the line identified by // WHAT?

    function Tokenizer(s) {
    this.tokens = [];
    this.tokenIndex = 0;
    this.source = s;
    }

    Tokenizer.prototype = {
    get token() { // WHAT?
    return this.tokens[this.tokenIndex];
    },

    ...

    get: function() {
    },

    };

    From the rest of the code, token is an object property of Tokenizer object instances. The goal of the above syntax appears to be for the value of the attribute to be set dynamically.

    I have spend some time searching javascript references on the web but I have not come up with a page that describes this. Does anyone have a pointer?

    Thanks!

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    58
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I beleive and I could very very easily be wrong, but I think there is supposed to be a period after the get in that line so it would look like get.token(). Because at the bottom of that code is get: function(), which to me says that there should be more to that part, and get.token() references that.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    it's a getter function, though the syntax is deprecated.

    alert(instance.token) would return this.tokens[this.tokenIndex];

    search for "javascript getters" for more info...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

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