coffeescriptコンパイラは、何らかの理由で、コンパイル時にすべての.coffeeファイルを関数内にラップしています。たとえば、私はtest.coffee持っている場合:関数内のCoffeescriptラッピングファイル
class TestClass
constructor: (@value) ->
printValue:() ->
alert(@value)
printAValue =() ->
test = new TestClass()
test.printValue()
その後、私が手test.jsを:
(function() {
var TestClass, printAValue;
TestClass = (function() {
function TestClass(value) {
this.value = value;
}
TestClass.prototype.printValue = function() {
return alert(this.value);
};
return TestClass;
})();
printAValue = function() {
var test;
test = new TestClass();
return test.printValue();
};
}).call(this);
私の単純なHTMLファイルはこれで動作しません。
<html>
<head>
<script src="test.js"></script>
</head>
<body onload="printAValue()">
</body>
</html>
I前にJSで多かったのですが、私はコーヒーコンパイラを疑っていませんが、うまくいくはずです。方法
あなたのcoffeescript行を変更して、TestClass初期化子に値を渡してみてください-'test = new TestClass( 'hello world') ' – arunkumar
JSファイル/モジュール間のコードの共有について[私の答えはこちら[1]を参照してください。 [1]:http://stackoverflow.com/questions/6951438/accessing-document-within-coffeescripts-default-wrapper/6951641#6951641 –
これは、これまででCoffeeScriptの上についての質問で最も人気のある質問ですそう。 http://stackoverflow.com/q/6481986/66226、http://stackoverflow.com/q/4214731/66226、http://stackoverflow.com/q/5693211/66226 ... –