简体   繁体   English

用coffeescript类返回一个

[英]Return an has with a coffeescript class

I want to wrap some pliugin options in a js class using Coffeescript. 我想使用Coffeescript在js类中包装一些pliugin选项。

In plain JS I have 用普通的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"
}

With Coffeescript 与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 {}. 当我检查toastr.options ,其空白为{}。 Why? 为什么?

Code below defines ToastrOptions class. 下面的代码定义ToastrOptions类。 It has toHash method defined which returns plain js object with predefined values. 它定义了toHash方法,该方法返回具有预定义值的纯js对象。 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. 参考您的代码-您将这个简单的对象定义直接放在构造函数方法中,这是行不通的,因为无论将什么放入构造函数方法中, new AnyClass的结果始终是此类的新实例。

Code below tested on http://coffeescript.org/ 以下代码在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