2014-05-01 34 views
0

我想寫遵循js代碼中的CoffeeScriptJS轉換爲CoffeeScript的

var test = (function(test) { 
    test.s = function(){ 
     console.log('hello') 
    } 
    return test; 
}(test || {})); 
test.s(); 

我用js2coffee。而我得到

test = ((test) -> 
    test.s = -> 
    console.log "hello" 
    return 

    test 
(test or {})) 
test.s() 

但這代碼不起作用/我在JS生成該CoffeeScript中我得到了另一個js代碼

var test; 

test = (function(test) { 
    test.s = function() { 
    console.log("hello"); 
    }; 
    return test; 
}, test || {}); // this line is different 

test.s(); 

你能幫助我。如何CoffeeScript的我的js腳本寫在正確

回答

0

這將編譯的方式一樣elclanrs do版本,並可能有助於闡明人的coffee2js說錯了

test = ((test) -> 
    test.s = -> 
    console.log "hello" 
    test) (test or {}) 
+0

謝謝你們兩位。所有解決方案都是對的 – user3592084

1

這是你想要什麼:

test = do (test=test or {}) -> 
    test.s = -> 
    console.log 'hello' 
    test 

輸出:

var test; 

test = (function(test) { 
    test.s = function() { 
    return console.log('hello'); 
    }; 
    return test; 
})(test || {});