在coffeescript中的函数末尾添加函数调用

时间:2012-02-09 04:30:16

标签: coffeescript ember.js

关于如何将其写为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')
});

我对代码的}.property部分特别感兴趣。我无法弄清楚如何用coffeescript写这个。

2 个答案:

答案 0 :(得分:6)

个人而言,我喜欢围绕我的职能:

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

我的脑袋可以更好地解析这个; - )

答案 1 :(得分:-1)

首先jsbeautifier,然后js2coffee

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

正如他们所说,让你的代码正确。