次のCoffeescriptコードを実行すると:
@sum = (x, y) -> x + y
私はこのコンパイルされたJavascriptを取得します:
(function() {
this.sum = function(x, y) {
return x + y;
};
}).call(this);
Coffeescriptに何かのような任意のオブジェクトthisに置き換える方法はありますか?.call(this)myObject
次のCoffeescriptコードを実行すると:
@sum = (x, y) -> x + y
私はこのコンパイルされたJavascriptを取得します:
(function() {
this.sum = function(x, y) {
return x + y;
};
}).call(this);
Coffeescriptに何かのような任意のオブジェクトthisに置き換える方法はありますか?.call(this)myObject
myobj.sum = (x, y) -> x + y
should get compiled to (UPDATE: See Rob W's answer for compile options) :-
myobj.sum = function(x, y) {
return x + y;
};
Isn't that what you want? So further you can call it using myobj.sum a, b
Complete code..
myobj = {}
myobj.sum = (x, y) -> x + y
alert(myobj.sum 10,4)
(function() {と}).call(this);はコンパイルの結果ではなく@sum = ...、実行可能ファイルによって追加されcoffeeます。これは、コンパイルの実際の結果です。
this.sum = function(x, y) {
return x + y;
};
別の/望ましい出力を取得するには、次のコードcoffee -b -cを使用して(またはcoffee -bcまたはcoffee --bare --compile) を実行します。
(->
@sum = (x, y) -> x + y
).call WHATEVER
になる
(function() {
return this.sum = function(x, y) {
return x + y;
};
}).call(WHATEVER);