Adding a function call on the end of function in coffeescript

牧云@^-^@ 提交于 2020-01-13 19:00:55

问题


Any ideas on how to write this as coffeescript?

Person = Ember.Object.extend({
  // these will be supplied by `create`
  firstName: null,
  lastName: null,

  fullName: function() {
    var firstName = this.get('firstName');
    var lastName = this.get('lastName');

   return firstName + ' ' + lastName;
  }.property('firstName', 'lastName')
});

I'm particularly interested in the }.property part of the code. I can't figure out how to write this in coffeescript.


回答1:


personally, i like braces around my functions:

Person = Ember.Object.extend(
  firstName: null
  lastName: null
  fullName: (->
    firstName = @get("firstName")
    lastName = @get("lastName")
    firstName + " " + lastName
  ).property("firstName", "lastName")
)

my head can better parse this ;-)




回答2:


first jsbeautifier it, then js2coffee it:

Person = Ember.Object.extend(
  firstName: null
  lastName: null
  fullName: ->
    firstName = @get("firstName")
    lastName = @get("lastName")
    firstName + " " + lastName
  .property("firstName", "lastName")
)

As they say,make your code right.



来源:https://stackoverflow.com/questions/9205434/adding-a-function-call-on-the-end-of-function-in-coffeescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!