this has so many use cases. Think of a facade of an array (anything arraylike really) or implementing binding.
On Mon, Nov 7, 2011 at 8:20 AM, Lasse Reichstein <reichsteinatw...@gmail.com> wrote: > On Mon, Nov 7, 2011 at 9:01 AM, Jarek Foksa <ja...@kiwi-themes.com> wrote: >> 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; >> }, >> } > > > This way of writing getters and setters in object literals was > introduced in ES5. > >> 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? > > If your object is being used by other code that expects to be able to > read startLine directly, using getters and setters allows you to have > side effects attached to reading it, without changing the existing > code to use getter and setter functions. > > Dumb getter and setter functions (those that really just get and set a > (private) variable) are really a modeling hack in anticipation of > maybe, possibly, once in the future to have to do something more. > 99.9% of getters/setters in Java could be made simple properties > without any problem. In languages with proper get/set attributes > (e.g., C# and now ECMAScript), you can make properties just > properties, and use get/set if you ever need something more. > > /L > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com > -- j:pn \\no comment -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com