while的用法
while((b=b.parentNode)){}是理解成b!==a; b=b.parentNode;!!?
while((b=b.parentNode)){}是理解成b!==a; b=b.parentNode;!!?
 
                            2015-11-14
判断循环是否需要继续进行的条件并不一定是b!==a 注意老师的代码写在了try catch块中 这里我写了个简单的测试
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>null or exception test</title>
</head>
<body>
    <h1 id="header">Test</h1>
</body>
<script>
    var node = document.getElementById("header");
 while(true){
        try{
            console.log(node = node.parentNode);
 }catch(e){
            console.log(e);
 break;
 }
    }
    console.log("-------------end------------");
</script>
</html>控制台运行结果如下

可以看到HTMLDocument的父节点为null,在老师的代码中,当【b=HTMLDocument】时,再执行while( (b=b.parentNode) ),即执行了while(null),此时循环条件不满足,程序不进入循环向下执行return false
另外通过test的运行结果可以看到对null使用parentNode会抛异常
所以你的问题while((b=b.parentNode)){}应当理解成 在不抛出异常的情况下,if( b || (b!==a) ) ; b=b.parentNode;
举报