微型的javascript REPL
时间:2010-09-09 来源:司徒正美
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);
">
p('You are at ' + location.href);
</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>
p('You are at ' + location.href);
相关阅读 更多 +










