2013-08-27 36 views
1

正如标题中所述:当从CoffeeScript编译类时,是否有强制使用JavaScript中的括号表示法的方法?从CoffeeScript编译为JavaScript时强制使用括号表示法

一个简单的例子是

的CoffeeScript

class test 

    myMethod:() -> 
     1 

编译的JavaScript没有括号符号

var test; 

test = (function() { 
    function test() {} 

    test.prototype.myMethod = function() { 
    return 1; 
    }; 

    return test; 

})(); 

编译的JavaScript带支架符号

var test; 

test = (function() { 
    function test() {} 

    test.prototype['myMethod'] = function() { 
    return 1; 
    }; 

    return test; 

})(); 

请注意,在第二个输出中,使用方括号表示方法myMethod()

我需要这个,以便我可以通过Google Closure Compiler运行输出,并且仍然保留我的方法的名称,这需要括号表示法,否则名称也会被缩小。

+0

我不记得谷歌关闭编译器...你需要它丑化和缩小? – cl0udw4lk3r

+0

@ cl0udw4lk3r和一些优化我的代码,是的。 – Sirko

回答

1

好吧,我可以在js2coffee看到,要完成,最好的办法是:

class test 
    'method': -> 
    console.log 'this is a test method' 

和U将得到这个js输出:

var Test; 

Test = (function() { 

    function Test() {} 

    Test.prototype['method'] = function() { 
    return console.log('this is a test method'); 
    }; 

    return Test; 

})(); 

顺便说一句,我建议为缩小/丑化使用Browserify