委譲とPeriodicalExecuterとprototype.jsと。

とりえあずサンプル。
次の様なHTMLと...

<html>
	<head>
	<script type="text/javascript" src="prototype.js"></script>
	<script type="text/javascript" src="mainctrl.js"></script>
	<script type="text/javascript">
		//<![CDATA[
		var mainctrl = new Mainctrl();
		// ]]>
	</script>
	</head>
	<body>
		<input type="button" onclick="mainctrl.start();" value="start"/><br/>
		<br/>
		<input type="button" onclick="mainctrl.stop();" value="stop"/><br/>
		<hr>
		<div id="status">
		</div>
	</body>
</html>

次の様なJavaScript

var Mainctrl = Class.create();
Mainctrl.prototype = {
	counter : 0,
	timer : null,

	initialize : function(){
		this.timer =  new PeriodicalExecuter(this.showCounter.bind(this), 2); // ← 委譲
		this.timer.stop();
	},

	start : function(){
		this.timer.registerCallback();
	},

	stop : function(){
		this.timer.stop();
	},

	showCounter : function(){
		this.counter = this.counter + 1;
		$('status').innerHTML = this.counter;
	}
};

で、ポイントは、

this.timer =  new PeriodicalExecuter(this.showCounter.bind(this), 2); // ← 委譲

である。
ここを、

this.timer =  new PeriodicalExecuter(this.showCounter(), 2);

この様に書くとエラーとなる。

this.callback is not a function

以後、後日へ続く。(あは