Ruby's Louvre

每天学习一点点算法

导航

微型的javascript REPL

REPL(Read Eval Print Loop)是一种非常有用的工具,能让我们即时运行代码得到结果。虽然相比于firebug的控制台来说,这简直不值一提,但在个人博客中提供演示时它还是比较有用。

下面是它的所有代码,包括html。

<textarea style="width:100%" onkeyup="
  this.rows=(function(t){
    for (var i=0,h=1;i<t.length;i++) if (t.charAt(i) == '\n') h++;
    return h;
  })(this.value);
">
//---------------------这是示例-----
var now = new Date;
var pad = function(num,  digits, radix, filling){
     num = num.toString(radix || 10);
     filling = filling || "0";
     while(num.length < digits){
         num= filling + num;
     }
     return num;
}
now = now.getFullYear()+"-"+pad(now.getMonth()+1,2)+"-"+pad(now.getDate(),2);
p(now);
//-----------------------------------
</textarea><input type="submit" value="Eval" onclick="
  try{
    var o = this.nextSibling.nextSibling;
    var p = function(s){
      var e = document.createElement('p');
      e.appendChild(document.createTextNode(s));
      e.style.margin=0;
      o.appendChild(e);
    };
    (new Function('p',this.previousSibling.value))(p);
  }catch(e){
    alert(e)
  }
"><input type="submit" value="Clear" onclick="
  this.nextSibling.innerHTML=''
"><div style="border:inset 1px;padding:0.5em"></div>

posted on 2010-09-09 00:47  司徒正美  阅读(2544)  评论(2编辑  收藏  举报