0
我有一个用CoffeeScript编写的非常简单的程序,如果用户点击一个按钮,应该在控制台中显示一个值。下面是我的代码:值没有被传递到类实例
HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id='butn'>click here</button>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
app.js是编译CoffeeScript的。我的CoffeeScript低于:
init.coffee
init = =>
game = new Game()
# Start it all off
$(document).ready(init)
game.coffee
class Game
constructor:() ->
@UI = new UI()
ui.coffee
class UI
constructor:() ->
@toolbar = new Toolbar('foo')
toolbar.coffee
class Toolbar
constructor: (@value) ->
@clickhandler()
clickhandler:() =>
$('body').on 'click', '#butn', ->
console.log 'Value = ', @value
编译的JS是:
// Generated by CoffeeScript 1.3.3
(function() {
var Game, Toolbar, UI, init,
_this = this,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
init = function() {
var game;
return game = new Game();
};
$(document).ready(init);
Game = (function() {
function Game() {
this.UI = new UI();
}
return Game;
})();
UI = (function() {
function UI() {
this.toolbar = new Toolbar('foo');
}
return UI;
})();
Toolbar = (function() {
function Toolbar(value) {
this.value = value;
this.clickhandler = __bind(this.clickhandler, this);
this.clickhandler();
}
Toolbar.prototype.clickhandler = function() {
return $('body').on('click', '#butn', function() {
return console.log('Value = ', this.value);
});
};
return Toolbar;
})();
}).call(this);
的问题
值 '富' 不被显示在控制台上。控制台记录“Value =”但不是“foo”。请有人帮助我理解为什么以及如何在不改变我的程序的情况下解决此问题。
感谢您的帮助。
谢谢你的帮助 – 2013-02-27 12:44:10