基本的に、 "@"変数はクラスのインスタンス変数、つまりクラスメンバーです。静的メンバーと比較できるクラス変数と混同しないでください。
また、あなたはOOP言語のthis
またはself
演算子として@variables
と考えることができますが、それは昔のjavascript this
とまったく同じものではありません。そのjavascript this
は現在のスコープを参照しています。これはコールバック内のクラススコープを参照しようとするときにいくつかの問題を引き起こします。そのためcoffescriptは@variables
を導入しています。 @
、これらの日は、あなたがクラスのインスタンス(すなわち、this
またはself
)を参照していることを意味し、
Brain.prototype = new EventEmitter();
function Brain(robot){
// Represents somewhat persistent storage for the robot. Extend this.
//
// Returns a new Brain with no external storage.
this.data = {
users: { },
_private: { }
};
this.autoSave = true;
var self = this;
robot.on('running', fucntion myCallback() {
// here is the problem, if you try to call `this` here
// it will refer to the `myCallback` instead of the parent
// this.resetSaveInterval(5);
// therefore you have to use the cached `self` way
// which coffeescript solved using @variables
self.resetSaveInterval(5);
});
}
決勝の思考:
はたとえば、次のコードを検討してください。したがって、@data
は基本的にはthis.data
を意味するので、@
がなければ、範囲内の可視変数data
を参照します。
coffeescript @はこれを意味します。 – HelloSpeakman
[CoffeeScriptのドキュメント](http://coffeescript.org)を見ましたか? '@'を検索することであなたの質問に答えることができ、おそらく他にもいくつかのことを教えてくれるでしょう。 –