2012年3月29日木曜日

開発環境

独習JavaScript 高橋 和也 (著), 竹添 直樹 (著), 里見 知宏 (著) の第7章(オブジェクト指向プログラミング)7.6(スコープと名前空間)理解度チェック3を解いてみる。

その他参考書籍

3.

コード(TextWrangler)

function AbstractStringSplitter(str,splitter){
  if(!str){
    throw new Error("文字列が指定されていません。");
  }
  this.str = str;
  this.splitter = splitter;
  this.array = this.getArray();
  this.i = -1;
}
AbstractStringSplitter.prototype.getArray = function(){
  return this.str.split(this.splitter);
};
AbstractStringSplitter.prototype.getNext = function(){
  this.i += 1;
  if(this.i == this.array.length){
    this.i = 0;
    return this.array[this.i];
  }
  return this.array[this.i];
};
function CommaStringSplitter(str){
  AbstractStringSplitter.apply(this,[str,","]);
};
CommaStringSplitter.prototype = new AbstractStringSplitter("tmp","");
delete CommaStringSplitter.prototype.str;
delete CommaStringSplitter.prototype.splitter;
delete CommaStringSplitter.prototype.array;
delete CommaStringSplitter.prototype.i;
CommaStringSplitter.prototype.constructor = CommaStringSplitter;
function SlashStringSplitter(str){
  AbstractStringSplitter.apply(this,[str,"/"]);
};
SlashStringSplitter.prototype = new AbstractStringSplitter("tmp","");
delete SlashStringSplitter.prototype.str;
delete SlashStringSplitter.prototype.splitter;
delete SlashStringSplitter.prototype.array;
delete SlashStringSplitter.prototype.i;
SlashStringSplitter.prototype.constructor = SlashStringSplitter;
var str = $('#t0').val();
var css = new CommaStringSplitter(str);
var sss = new SlashStringSplitter(str);
var splitters = [css,sss];
var result = "";
for(var i = 0 ; i < splitters.length ; i++){
  var array = splitters[i].getArray();
  for(var j = 0 ; j < array.length ; j++){
    result += j + ": " + array[j] + "<br >";
  }
  for(var k = 0 ; k < array.length * 2 ; k++ ){
    result += splitters[i].getNext() + "<br />";
  }
}
$('#pre0').html(result);






						

0 コメント:

コメントを投稿