代码改变世界

两个无法理解的 JS 错误

2007-10-09 03:16  晓风残月  阅读(1262)  评论(1编辑  收藏  举报
两个无法理解的JS错误,以及解决方案:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>Untitled Page</title>
    
<script type ="text/javascript">
    
var a;
    
var $1 = document.getElementById;
    
var $2 = function(id) return document.getElementById(id); }
    
function foo() {
        
//
        alert(a);
        alert(
!a); 
        
//alert(b); // ERROR 'b' 未定义
        alert(window.b); // OK
        //alert(!b); // ERROR 'b' 未定义
        alert(!window.b); // OK
        //
        // OK: IE, 
        // ERROR:   FireFox 错误: uncaught exception: [Exception "Illegal operation on WrappedNative prototype object"  nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)"  location: "JS frame :: http://localhost/Digdotnet.Test/JSSample/Dig_$_undefined.htm :: foo :: line 18"  data: no]
        //          Opera Unhandled exception: [Object InternalException]  message: WRONG_THIS_ERR
        //var o1 = $1("none"); 
        //alert(o1);
        var o2 = $2("none");  // OK
        alert(o2);
    }

    window.onload 
= function() { foo(); }
    
</script>
</head>
<body>

</body>
</html>