Getters and Setters

170 views
Skip to first unread message

yu...@ualberta.ca

unread,
Aug 27, 2013, 3:42:52 AM8/27/13
to lives...@googlegroups.com
Does LiveScript support getters and setters in object literals?

George Zahariev

unread,
Aug 27, 2013, 3:33:04 PM8/27/13
to lives...@googlegroups.com
Look at the end of the http://livescript.net/#oop - it supports shorthand for Object.defineProperty

Yuri Delanghe

unread,
Aug 27, 2013, 7:41:46 PM8/27/13
to lives...@googlegroups.com
I'm aware of that. I'm not using them in a class or with Object.defineProperty, I'm using them in an object literal. For now I've resorted to writing it in literal JavaScript `` ``; can support for this be added to LiveScript?

George Zahariev

unread,
Aug 27, 2013, 8:04:47 PM8/27/13
to lives...@googlegroups.com
Hmm, apparently it is not documented correctly. Try using the same syntax on an object literal, and you'll get what you want.

obj =
  dimensions
:~
   
-> @d
   
([width, height]) -> @d = "#{width}x#height"

==>

var obj;
obj = {
  get dimensions(){
    return this.d;
  },
  set dimensions(arg$){
    var width, height;
    width = arg$[0], height = arg$[1];
    this.= width + "x" + height;
  }
};

Yuri Delanghe

unread,
Aug 27, 2013, 8:11:11 PM8/27/13
to lives...@googlegroups.com
Perfect, thank you!
Reply all
Reply to author
Forward
0 new messages