Вопрос

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

Это было полезно?

Решение 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 {})

Другие советы

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 || {});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top