正如标题中所述:在从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 :(得分:1)
我可以在js2coffee看到,最好的方法是:
class test
'method': ->
console.log 'this is a test method'
你将得到这个js输出:
var Test;
Test = (function() {
function Test() {}
Test.prototype['method'] = function() {
return console.log('this is a test method');
};
return Test;
})();
顺便说一句,我建议缩小/使用Browserify。