While reading WebKit Inspector's sources I have stumbled upon syntax
that I have not seen before, namely get & set keywords:

var editor = {
  _startLine: 91,

  get startLine() {
    console.log('Getting start line');
    return this._startLine;
  },

  set startLine(value) {
    console.log('Setting start line');
    this._startLine = value;
  },
}

> editor.startLine = 12
>> Setting start line
>> 12
> editor.startLine
>> Getting start line
>> 12

I fail to see what's the point of this syntax. Considering the fact
that it's still possible to change the value of editor._startLine
without the setter, how is that better than using regular functions
like below?

var editor = {
  _startLine: 91,

  getStartLine: function() {
    console.log('Getting start line');
    return this._startLine;
  },

  setStartLine: function(value) {
    console.log('Setting start line');
    this._startLine = value;
  },
}

> editor.setStartLine(12)
>> Setting start line
> editor.getStartLine()
>> Getting start line
>> 12

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to