Skip to content
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

Closed
c5n8 opened this issue Oct 14, 2017 · 9 comments
Closed

How to produce static getter for a class property? #4746

c5n8 opened this issue Oct 14, 2017 · 9 comments
Labels

Comments

@c5n8
Copy link

c5n8 commented Oct 14, 2017

Like this

class User extends Model {
  static get table () {
    return 'my_users'
  }
}
@GeoffreyBooth
Copy link
Collaborator

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'

@c5n8
Copy link
Author

c5n8 commented Oct 16, 2017

Ok, thanks, I'll try it. I appreciate your awesome works on coffeescript.
Anyway, what is your argument against getters and setters?

@GeoffreyBooth
Copy link
Collaborator

@c5n8
Copy link
Author

c5n8 commented Nov 9, 2017

Sorry to bother again, but I cannot produce this

class User extends Model {
  static get dates () {
    return super.dates.concat(['dob'])
  }
}

Any idea?

@c5n8
Copy link
Author

c5n8 commented Nov 9, 2017

Anyway, I still cant implement your previous example, and I end up set it as property like this

class User extends Model
  @hidden = [ 'password' ]

@GeoffreyBooth
Copy link
Collaborator

GeoffreyBooth commented Nov 9, 2017

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 User.getDates() rather than User.dates.

@c5n8
Copy link
Author

c5n8 commented Nov 9, 2017

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.
I hope there will be better interoperability with existing js lib, regardless what practice they use.

@GeoffreyBooth
Copy link
Collaborator

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?

@c5n8
Copy link
Author

c5n8 commented Nov 9, 2017

Yeah, that works too, thanks.
It is Adonis Framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants