2016-08-20 7 views
0

Coffeescriptを使用してjsクラスでいくつかのpliuginオプションをラップしたいと思います。私はCoffeeScriptのcoffeescriptクラスを持つhasを返します

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

を有する普通JSで

Iはtoastr.optionsをチェックするとザは} {ブランクでいます。どうして?

+0

あなたのjsの上でテスト

以下のコードは何のクラスではありません、それはオブジェクトリテラルですので、なぜあなたは 'のCoffeeScriptとclass'を使用しています? – Bergi

答えて

0

以下のコードは、ToastrOptionsクラスを定義しています。定義済みの値を持つプレーンjsオブジェクトを返す、toHashメソッドが定義されています。 コードを参照してください。コンストラクタメソッドに何を入れても、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()) 
関連する問題