所以简短的版本,我不明白的是这行代码:
(new Function("paper", "window", "document", cd.value)).call(paper, paper);
长版,看看这些功能:
window.onload = function () {
var paper = Raphael("canvas", 640, 480);
var btn = document.getElementById("run");
var cd = document.getElementById("code");
(btn.onclick = function () {
paper.clear();
paper.rect(0, 0, 640, 480, 10).attr({fill: "#fff", stroke: "none"});
try {
(new Function("paper", "window", "document", cd.value)).call(paper, paper);
} catch (e) {
alert(e.message || e);
}
})();
};
这段代码来自 Raphael playground,这意味着它实现了 raphael 库。因此,我不理解顶部的单行代码(它在 try/catch 表达式中),假设将用户输入的存储在 cd.value 中的代码复制到函数中。但这怎么可能?
您可以在此处访问该页面:http ://raphaeljs.com/playground.html