访问JavaScript对象的构造函数时失败

    在JavaScript定义的对象中,不管是内部对象,还是用户自定的对象。如果该对象是从模态窗口(Modal Dialog)中创建并返回到主窗口中的,我们将无法在主窗口中取到该对象的构造函数(constructor)。

    执行如下两个示例:
    1、Main.htm

<html>
<head>
    
<title>Main Page</title>
    
<meta name="author" content="birdshome@博客园" />
</head>
<body>
    
<button onclick="GetValueFromDialog()">
        click
</button>
    
<script>
    
var m_Array = []; 
    
function GetValueFromDialog()
    
{
         
var array = window.showModalDialog('dialog.htm');
         alert(array.constructor);
         
// alert(new m_Array.constructor);
         // alert(new array.constructor);
    }

   
</script>
</body>
</html>

    2、Dialog.htm
<html>
<head>
    
<title>Modal Dialog</title>
    
<meta name="author" content="birdshome@博客园" /> 
</head>
<body>
    
<script>
    
function ReturnValue()
    
{
         window.returnValue 
= ['modal dialog'];
         
// window.returnValue = new function foo(){}; 
         window.close();
    }
   
    
</script>
   
<button onclick="ReturnValue()">close</button> 
</body>
</html>

    关闭弹出窗口dialog.htm,执行alert(array.constructor);将会引发脚本运行时异常:
 A Runtime Error has occurred.
 Do you wish to Debug
?

 Line: 
12
 Error: Unexpected call to method or property access.
    // Unable to evaluate the expression. Catastrophic failure

    不过在这里JavaScript的内部对象和用户自定义对象还有一点小区别,如果是JS内部对象,我们访问对象的构造函数就立即出错。alert(array.constructor)就异常了。而如果是用户指定一对象,我们可以执行alert(array.constructor)得到一个显示"[object]"的MsgBox,但是这时的contrutor仍然不支持任何的操作和执行任何方法,比如new、.toString()等,一旦执行就出和上面一样的异常"Unable to evaluate the expression. Catastrophic failure"。

    这个缺陷似乎对我们的影响不是很大,不过对于一些依赖于对象的constructor来实现的功能就郁闷了,比如获取用户自定义对象的名称JavaScript面向对象编程之Singleton类、以及我上次做的一个对象Clone方法等,在这中情况下就都歇菜了

posted on 2005-04-01 00:39  birdshome  阅读(2814)  评论(0编辑  收藏  举报

导航