Is this valid Javascript syntax? What does it do?
Parser.prototype = {
// ...
get currentState() {
return this.state[this.state.length - 1];
},
// ...
}
See https://github.com/LearnBoost/stylus/blob/master/lib/parser.js.
Thank you!
It defines a getter:
Read about Getters and Setters.
This function is called when you access the property:
Notice that it is not a function call (there are no
()), but a normal property access.A corresponding setter would look like this:
and would be called when you assign a value to that property, e.g.
The
getandsetkeywords a special operators to be used inside the object literal notation. You could also use__defineGetter__and__defineSetter__:I’m not sure in which version it was introduced though, it might not be supported by all browsers (especially IE ;)).