Syntax Strange JavaScript Object

I recently got 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 am familiar with JS, but that seems pointless. Chrome interprets this as an Object with $ h.HEAD, and the rest as numbers that appear after the return. In addition, something like getters is also part of $ h.

Can someone give me more info about this? IE cannot interpret this, so I have to do the equivalent of this.

+4
source share
1 answer

You have found getters .

With getter, you can execute a function when you read an object property. It is also a way to give some protection to the properties of an object; you cannot directly change the getter value.

Getters IE IE9.

+7

All Articles