Kamimura's blog
プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
ほしい物リスト
2010年6月6日日曜日
プログラミング学習の記録 213 独習JavaScript (大型本)の第7章(オブジェクト指向プログラミング)の理解度チェック3
開発環境:
Mac OS X Snow Leopard (OS)
Firefox(ブラウザ)+Firebug(アドオン)
"独習JavaScript (大型本)"のp.249, 第7章(オブジェクト指向プログラミング)の理解度チェック3を解いてみる。
3.
function AbstractStringSplitter(str, spl) { this.str = str; this.spl = spl; this.array = this.getArray(); this.pos = -1; } AbstractStringSplitter.prototype.getArray = function () { return this.str.split(this.spl); }; AbstractStringSplitter.prototype.getNext = function () { this.pos += 1; if (this.pos >= this.array.length) { this.pos = -1; return null; } return this.array[this.pos]; }; function SlashStringSplitter(str) { AbstractStringSplitter.apply(this, [str, "/"]); } function CommaStringSplitter(str) { AbstractStringSplitter.apply(this, [str, ","]); } SlashStringSplitter.prototype = new AbstractStringSplitter("tmp", "/"); delete SlashStringSplitter.prototype.str; delete SlashStringSplitter.prototype.spl; delete SlashStringSplitter.prototype.array; delete SlashStringSplitter.prototype.pos; SlashStringSplitter.prototype.constructor = SlashStringSplitter; var str = prompt("文字列を入力してください", ""); var array = new SlashStringSplitter(str); alert("/で区切った配列の先頭の要素: " + array.getNext()); CommaStringSplitter.prototype = new AbstractStringSplitter("tmp", ","); delete CommaStringSplitter.prototype.str; delete CommaStringSplitter.prototype.spl; delete CommaStringSplitter.prototype.array; CommaStringSplitter.prototype.constructor = CommaStringSplitter; var array1=new CommaStringSplitter(str); alert(",で区切った配列の先頭の要素: "+ array1.getNext());
0 コメント:
コメントを投稿
次の投稿
前の投稿
ホーム
コメントの投稿(Atom)
0 コメント:
コメントを投稿