Return 有一个 coffeescript class

Return an has with a coffeescript class

我想使用 Coffeescript 在 js class 中包装一些插件选项。

在普通 JS 中我有

toastr.options = {
  "closeButton" : false,
  "debug" : false,
  "positionClass" : "toast-bottom-right",
  "onclick" : null,
  "showDuration" : "300",
  "hideDuration" : "1000",
  "timeOut" : "8000",
  "extendedTimeOut" : "1000",
  "showEasing" : "swing",
  "hideEasing" : "linear",
  "showMethod" : "fadeIn",
  "hideMethod" : "fadeOut"
}

使用 Coffeescript

class @ToastrOptions
  constructor: ->
    'closeButton': false
    'debug': false
    'positionClass': 'toast-bottom-full-width'
    'onclick': null
    'showDuration': '300'
    'hideDuration': '1000'
    'timeOut': '8000'
    'extendedTimeOut': '1000'
    'showEasing': 'swing'
    'hideEasing': 'linear'
    'showMethod': 'fadeIn'
    'hideMethod': 'fadeOut'

 toastr.options = new ToastrOptions

当我检查 toastr.options 时,{} 是空白的。为什么?

下面的代码定义了 ToastrOptions class。它定义了 toHash 方法,其中 returns 具有预定义值的普通 js 对象。 参考您的代码 - 您将这个普通对象定义直接放在无法工作的构造函数方法中,因为 new AnyClass 的结果始终是此 class 的新实例,无论您在构造函数中放入什么方法。

下面的代码在 http://coffeescript.org/

上测试
class ToastrOptions
  toHash: ->
    closeButton: false
    debug: false
    positionClass: 'toast-bottom-full-width'
    onclick: null
    showDuration: '300'
    hideDuration: '1000'
    timeOut: '8000'
    extendedTimeOut: '1000'
    showEasing: 'swing'
    hideEasing: 'linear'
    showMethod: 'fadeIn'
    hideMethod: 'fadeOut'


console.log(new ToastrOptions().toHash())