【问题标题】:Adding a function call on the end of function in coffeescript在咖啡脚本中的函数末尾添加函数调用
【发布时间】:2012-03-01 14:28:41
【问题描述】:

关于如何将其编写为咖啡脚本的任何想法?

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写这个。

【问题讨论】:

标签: coffeescript ember.js


【解决方案1】:

就我个人而言,我喜欢在我的函数周围使用大括号:

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

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

【讨论】:

    【解决方案2】:

    首先是jsbeautifier它,然后是js2coffee它:

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

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

    【讨论】:

    • 为什么投反对票?这个答案与 Michael Siebert 的答案基本相同,并解释了作者使用的过程。我错过了什么吗?
    • 我的代码有一点错误。我应该在fullName'value 周围使用括号,这是一个function
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    相关资源
    最近更新 更多