什么时候JavaScript对象被破坏? [英] When are JavaScript objects destroyed?

查看:113
本文介绍了什么时候JavaScript对象被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我可以显式定义构造函数和析构函数,然后cout<<在构造函数/析构函数中使用C或D调用,确切地知道在哪里。



但是在JavaScript中如何知道对象何时被破坏。以下示例是关注我的情况。



我在超时时调用内部函数,我想知道对象是否保持活动状态只要定时器正在运行,等待再次拨打电话。



用户点击通话控制

  //呼叫控制

控制呼叫消息

  var message_object = new Message(response_element); 

消息来电效果

  new Effects()。fade(this.element,'down',4000); 
message_object.display('empty');

效果

  / ** 
*效果 - 根据需要构建
*元素 - 保持元素淡出
*方向 - 确定哪种方式淡化element
* max_time - 淡入淡出的长度
* /

var效果= function()
{
this.fade = function(element,direction ,max_time)
{
element.elapsed = 0;
clearTimeout(element.timeout_id);
函数next()
{
element.elapsed + = 10;
if(direction ==='up')
{
element.style.opacity = element.elapsed / max_time;
}
else if(direction ==='down')
{
element.style.opacity =(max_time - element.elapsed)/ max_time;
}
if(element.elapsed< = max_time)
{
element.timeout_id = setTimeout(next,10);
}
}
next();
}
};


解决方案

JS对象本身没有析构函数。



JavaScript对象(和原语)在不可访问时被垃圾收集,这意味着当前执行上下文中没有可能的引用它们时。 JavaScript运行时必须持续监视此情况。因此,除非您使用 delete 关键字删除某些内容,否则它的破坏程度有点暴力。有些浏览器在检测闭包范围内留下的引用时很糟糕(我正在看你,Redmond)这就是为什么你经常看到在函数结束时将对象设置为null的原因 - 以确保在IE中释放内存。 / p>

In C++ I can define a constructor and destructor explicitly, and then cout << "C or D Called" from with in the constructor/destructor function, to know exactly where.

However in JavaScript how do I know when an object is destructed. The example below is the case that concerns me.

I'm calling an internal function on a timeout and I'm wondering if the object stays alive as long as the timer is running, waiting to call next again.

User Click calls Control

// Calls  Control

Control calls Message

var message_object = new Message( response_element );

Message calls Effects

new Effects().fade( this.element, 'down', 4000 );
message_object.display( 'empty' );

Effects

/**
 *Effects - build out as needed
 *  element - holds the element to fade
 *  direction - determines which way to fade the element
 *  max_time - length of the fade
 */

var Effects = function(  ) 
{
    this.fade = function( element, direction, max_time ) 
    {
        element.elapsed = 0;
        clearTimeout( element.timeout_id );
        function next() 
        {
            element.elapsed += 10;
            if ( direction === 'up' )
            {
                element.style.opacity = element.elapsed / max_time;
            }
            else if ( direction === 'down' )
            {
                element.style.opacity = ( max_time - element.elapsed ) / max_time;
            }
            if ( element.elapsed <= max_time ) 
            {
                element.timeout_id = setTimeout( next, 10 );
            }
        }
        next();
    }
};

解决方案

JS objects don't have destructors per se.

JavaScript objects (and primitives) are garbage collected when they become inaccessible, meaning when there is no possible reference to them in the current execution context. The JavaScript runtime has to continuously monitor for this. So unless you use the delete keyword to remove something, then its destruction is sort of under the hood. Some browsers are bad at detecting references left in closure scope (I'm looking at you, Redmond) and that's why you often see objects being set to null at the end of functions--to make sure that memory is freed in IE.

这篇关于什么时候JavaScript对象被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆