-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to produce static getter for a class property? #4746
Comments
We treat getters and setters as a discouraged practice. See http://coffeescript.org/#unsupported-get-set. That said, it’s possible to create a getter for a class using the verbose syntax, and such a getter is automatically available statically: class User extends Model
Object.defineProperty User, 'table', {get: -> 'my_users'}
User.table # 'my_users' |
Ok, thanks, I'll try it. I appreciate your awesome works on coffeescript. |
Sorry to bother again, but I cannot produce this class User extends Model {
static get dates () {
return super.dates.concat(['dob'])
}
} Any idea? |
Anyway, I still cant implement your previous example, and I end up set it as property like this class User extends Model
@hidden = [ 'password' ] |
Getters and setters are a discouraged practice in JavaScript. Instead I would advise: class User extends Model
@getDates: ->
super.dates.concat ['dob'] which becomes: User = class User extends Model {
static getDates() {
return super.dates.concat(['dob']);
}
}; and call it via |
This is what I use in the end: class User extends Model
@dates: @getDates()
@getDates: -> super.dates.concat(['period']) The thing is it is required by framework I use, so I have no other choice. |
You can create getters and setters using the verbose syntax, as shown in the docs: http://coffeescript.org/#unsupported-get-set. class Model
@dates: ['era']
class User extends Model
@getDates: -> super.dates.concat(['period'])
Object.defineProperty User, 'dates',
get: -> @getDates()
alert User.dates # era,period What framework are you using that “requires” getters and setters? |
Yeah, that works too, thanks. |
Like this
The text was updated successfully, but these errors were encountered: