为Safari开发javascript——防止死亡之球

为Safari开发javascript——防止死亡之球,javascript,debugging,safari,crash,Javascript,Debugging,Safari,Crash,我发现自己写了很多像下面这样的代码,但仍然有很多死机。它使整个代码编写过程变得更加痛苦,这是它应该做的。还有更好的办法吗 function drawArrowsInDocument (document, boxes, context) { console.log ("drawArrowsInDocument"); var body = document.body; if (!body) { console.log ("no body!");

我发现自己写了很多像下面这样的代码,但仍然有很多死机。它使整个代码编写过程变得更加痛苦,这是它应该做的。还有更好的办法吗

function drawArrowsInDocument (document, boxes, context) {
    console.log ("drawArrowsInDocument");
    var body = document.body;
    if (!body) {
        console.log ("no body!");
        return;
    }
    var rectangles = rectanglesWithBoxes(boxes);
    if (!rectangles) {
        console.log ("no rectangles!");
        return;
    }
    var descendants = body.childNodes;
    if (!descendants) {
        console.log ("no descendants");
        return;
    }
    var length = descendants.length;
    if (length>10000) {
        console.log ("too many descendants");
        return;
    }
    // now I know my variables actually exist, and I can do something with them.
这是我在它崩溃时看到的。在这一点上,我唯一能做的就是重新启动Safari,并输入更多的if语句来发现问题

使用而不是console.log。有内置的
控制台。断言

console.assert(Number("a"),"Not a Number",Number("a"))  
可以帮助将测试与代码分开,以避免在部署之前删除控制台语句

参考资料


我发现Chrome不太稳定,而且几乎是同一台发动机。您还可以使用
throw
随时停止脚本。死亡之球在无限循环中最常见,所以重新考虑那些while循环。