2

I recently came accross this in a Library called WebApp.net:

var $h = {
    get HEAD() { return 0 },
    get BACK() { return 1 },
    get HOME() { return 2 },
    get LEFT() { return 3 },
    get RIGHT() { return 4 },
    get TITLE() { return 5 }
};
var $d = {
    get L2R() { return +1 },
    get R2L() { return -1 }
};

I'm familar with JS, but this seems to make no sense. Chrome does interpret this as Object with $h.HEAD and the others set as numbers which come after the return. Additionally, something like getters are part of $h, too.

Can anybody give me more info about this? IE cannot interpret this, so I have to make an equivalent to this.

2
  • 4
    It's an object containing getters. Works also in IE>8. Commented May 25, 2015 at 15:31
  • 2
    @Teemu: make it an answer Commented May 25, 2015 at 15:43

1 Answer 1

6

You've found getters.

With a getter you can execute a function, when you're reading a property of an object. Also it is a way to give some protection to object properties, you can't change the value of a getter directly.

Getters and setters are implemented also in IE since IE9.

Sign up to request clarification or add additional context in comments.

2 Comments

You're right, thanks. I was surprised to came accross such a "modern" feature in such an old code. My IE problem was because of compat mode in IE11 wich is mostly like IE8. There it's not available.
@sja I don't know, how old WebApp.net is, but in FF getters and setters are supported since 2.0, which has been released in 2006.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.