Question

I'm trying to write follow js code in coffeescript

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

I use js2coffee. And what I get

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

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

But this code doesn't work/ When I generated this coffeescript in js I got another js code

var test;

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

test.s();

Can you help me out. How write in coffeescript my js script properly

Was it helpful?

Solution 2

This compiles the same way as elclanrs do version, and may help clarify were the coffee2js got it wrong

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

OTHER TIPS

This is what you want:

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

Output:

var test;

test = (function(test) {
  test.s = function() {
    return console.log('hello');
  };
  return test;
})(test || {});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top