簡體   English   中英

用coffeescript類返回一個

[英]Return an has with a coffeescript class

我想使用Coffeescript在js類中包裝一些pliugin選項。

用普通的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類。 它定義了toHash方法,該方法返回具有預定義值的純js對象。 參考您的代碼-您將這個簡單的對象定義直接放在構造函數方法中,這是行不通的,因為無論將什么放入構造函數方法中, new AnyClass的結果始終是此類的新實例。

以下代碼在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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM