概要
thisの束縛
詳細
以下の記法で関数定義thisの束縛して、関数の外部のthisを関数内部に
引き継ぐことができます。
instance_name = => // concrete logic
Coffee Script
class Hoge name: "hoge" test: -> sub_hoge = => console.log "#{@name}" sub_hoge() new Hoge().test()
JavaScript
<script language="javascript"> var Hoge; Hoge = (function() { function Hoge() {} Hoge.prototype.name = "hoge"; Hoge.prototype.test = function() { var sub_hoge, _this = this; sub_hoge = function() { return console.log("" + _this.name); }; return sub_hoge(); }; return Hoge; })(); new Hoge().test(); </script>
出力
hoge