2014-05-01 35 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 || {});