Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304993
  • 博文数量: 174
  • 博客积分: 3061
  • 博客等级: 中校
  • 技术积分: 1740
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-04 22:43
文章分类

全部博文(174)

文章存档

2011年(54)

2010年(14)

2009年(30)

2008年(26)

2007年(27)

2006年(23)

我的朋友

分类: WINDOWS

2011-07-08 11:20:23

最近做一个项目,碰到了这样一个问题,有两个frame,在不同的page中。

比如global.html 

var g_outobj;   // ref var from frame.html
    function test(obj)  // obj 来自于frame.html
    {
    g_outobj = obj;
    g_outobj.str = "changed";  // 我尝试修改它
     }
   
    function dump()
    {
     alert(g_outobj.getstr());  // 看看这个str现在是多少
    var obj = g_outobj.checkself(); // 看看对象所在的window是否还存在
     }
   
   
     }

in frame.html
function thisobj()
    {
    this.str = "this is from frame obj";
   
    this.test = function()
    {
    alert(this.str);
    }
   
    this.getstr = function()
    {
    return this.str;
    }
   
    this.checkself = function()
    {
   
    return typeof(window.alert) + typeof(this.str);
    }
    }
   
    var o = new thisobj();
   
     globalctx.test(o);
    alert("after call globalctx.test " + o.str);
   
    delete o;
        o = null;

        globalctx.dump();


 通过这个例子,我得出如下结论:

1. js的变量是带有引用计数的或者类似,即时引用的obj来自于另外的frame(比如global.html)。  这个表示啥呢? 像上例的o 这个对象,即时所在的frame页面被干掉了,那么只要global还在引用这个变量,那么这个js变量就依然存在,无非创建它的window上下文已经消失了。

2. 两个frame实际访问这个对象o的时候,确实是操作了同一个对象,这个可以从修改后str的表现看出。

3. 当frame被干掉后,global中继续访问o or g_outobj 依然可以,只是这个g_outobj 的执行上下文依然在那个已经消失的window中,所以一旦访问window就会出错,比如typeof window就会返回undefined。
阅读(796) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~