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