简体   繁体   中英

Return an has with a coffeescript class

I want to wrap some pliugin options in a js class using Coffeescript.

In plain JS I have

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"
}

With 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

When I check toastr.options the has is blank {}. Why?

Code below defines ToastrOptions class. It has toHash method defined which returns plain js object with predefined values. Referring to your code - you put this plain object definition directly in constructor method which couldn't work, because a result of new AnyClass is always a new instance of this class, no matter what you put in constructor method.

Code below tested on 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM