0
var bookmark_iterator = page_element.firstChild;
do {

    // start insertion

    if (bookmark_iterator === null) {
        page_element.appendChild(div_el);
        break;
    }

    // middle insertion

    if (div_el.id < bookmark_iterator.id) {
        page_element.insertBefore(div_el, bookmark_iterator);
        break;
    }

    // end insertion

    if (bookmark_iterator === page_element.lastChild) {

        // if null will insert at the end per reference

        bookmark_iterator = null;
        page_element.insertBefore(div_el, bookmark_iterator);
        break;
    }

    // increment loop

    bookmark_iterator = bookmark_iterator.nextSibling;
} while (bookmark_iterator !== null);

问题是http://jslint.com抛出错误:

line 643 character 19
Unexpected 'else' after disruption.

因为我的 if/else 结构中有一个 break 语句。

这让我觉得我把一个简单的字母插入太复杂了。有没有更简单或更好的方法来编写它以使其通过 jslint?

4

1 回答 1

3

只是摆脱else那些块之后。JSLint 试图告诉您的是,在将控制权转移到周围块语句之外的块else之后使用是没有意义的。if它在语法上没问题,但它是,嗯,愚蠢的。仅当控制流从块继续到后续代码时,使用elseafter an才有用。既然你在那里有陈述,那将永远不会发生。ififbreak

如果没有更多上下文,很难说您的整体逻辑是否可以改进。

于 2013-05-27T15:45:25.443 回答