tc39 / proposal-class-public-fields

Stage 2 proposal for public class fields in ECMAScript
https://tc39.github.io/proposal-class-public-fields/
487 stars 25 forks source link

Computed properties #14

Closed PinkaminaDianePie closed 9 years ago

PinkaminaDianePie commented 9 years ago

Need more description about computed properties. It's a very important thing for class instance fields, case it only way to use Symbol. Ill explain it on usecase in my project. I'm writing simple orm with java-like declarations, i.e. in Hibernate framework. In this case all fields in data model and links between them are described as instance fields with decorators:

class User extends ActiveRecord{
  id = 0
}

class Group extends ActiveRecord{
  id = 0
}

class Photo extends ActiveRecord{
  @manyToOne(User, 'id')
  userId = null

  @manyToOne(Group, 'id')
  groupId = null

  dateAdd = new Date()
}

What about ActiveRecord class? it needs to store computed metadata about DB schema, about fields, changed by user and needed to save (for example this information can be collected by proxy object). But i dont want to show this metadata by toString() or JSON.stringify() methods. I want to show user only its own data. So ill store this metadata in Symbol, and this metadata needed for each model instance. So ActiveRecord needs to be something like this:

class ActiveRecord{
  [framework.symbols.modelMetadata] = {
    ...
  }
}

As instance fields is declarative way to describe fields, they will be often used with class decorators in libraries and frameworks, so many people will used them with symbols to hide frameworks metadata. I request better description with using instance fields with computed properties.

jeffmo commented 9 years ago

Yes, we need to expand the grammar to allow computed field names. I'm going to try and work on updating the proposal this week (in prep for the meeting next week) and I'll include this in those updates.

(leaving this issue open as a tracker)

michaelficarra commented 9 years ago

@jeffmo What needs to be expanded? The PropertyName production is being used currently, and that allows computed names.

jeffmo commented 9 years ago

Oh you're right, I forgot that was changed already :p

So then my comment above is outdated and this is already possible as specified here: https://github.com/jeffmo/es-class-static-properties-and-fields#145-class-definitions