从函数返回的对象的生命周期

时间:2014-01-30 09:21:42

标签: javascript garbage-collection module-pattern

使用module-esq模式允许链接方法时,返回的对象在被垃圾回收之前会持续多长时间?我真的很喜欢jquery允许链接方法的方式,但是我担心如果我将代码用于我的代码,它会污染带有大量不必要对象的页面内存。

这是一个简单的例子

(function(){
    // persistant variable wrapped in a closure
    var prop = 'something';

    //module function
    function house(){
        function method1(){
            console.log(prop);
            return this;
        }
        function method2(value){
            prop = value
            return this;
        }
        return {
            getProp: method1,
            setProp: method2
        }
    }
    window.house = house;
})();

/*
 * I am wanting to know how long the returned object will last in memory
 */

house().setProp('somethingElse');

以下是更真实的例子:

(function(){
    // object cache to store all the elem id's that have been called
    var _elemIds = {};

    function get(elem){

        if(!_elemIds[elem]){
            // add to _elemIds object if doesn't exist
            _elemIds[elem] = document.getElementById(elem);
        }

        var _currentElem = _elemIds[elem];

        // set a css property using a json object, or get with a string
        function css(){
            if(typeof arguments[0] === 'object'){
                for( x in arguments[0]){
                    _currentElem.style[x] = arguments[0][x];
                }
                return this;
            }
            else if(typeof arguments[0] === 'string'){
                var l = arguments.length;
                // if more than one argument return an array
                if(l > 1){
                    var ret = [];
                    for (var i = 0; i < l; i++) {
                        if(_currentElem.style[arguments[0]] !== ''){
                            ret.push(_currentElem.style[arguments[i]]);
                        } else {
                            ret.push(window.getComputedStyle(_currentElem, null)[arguments[i]]);
                        }
                    }
                    return ret;
                } else {
                    if(_currentElem.style[arguments[0]] !== ''){
                        return _currentElem.style[arguments[0]];
                    } else {
                        return window.getComputedStyle(_currentElem, null)[arguments[0]];
                    }   
                }   
            }
        }

        // change the color of the text
        function color(color){
            _currentElem.style.color = color;
            return this;
        }

        // log the current element
        function test(){
            console.log('currentElem id: ' + _currentElem.id);
            return this;
        }

        // return the public methods
        return {
            css: css,
            current: test,
            color: color
        }
    };
    // set the get method to the global object
    window.get = get;

})();

从上面的代码访问方法,你会使用像

这样的东西
get('elemId').css(('prop': 'value'}).current().css('prop');

感谢您的回答。

干杯,

安德鲁

1 个答案:

答案 0 :(得分:1)

首先,您应该阅读这个问题:Learning garbage collection theory

然后,您需要知道每个javascript运行时都实现了自己非常具体的GC,因此对于何时以及如何对对象进行垃圾回收,绝对无规则。一旦它们没有被引用,你应该认为它们永远消失了,并且相信(我知道,要求很多)GC会在对象被解除引用后的“最佳时间”释放内存。

要了解有关每个GC的具体内容的更多信息,您需要阅读有关每个GC的资源,幸运的是,现在只有3个主要引擎!以下资源可帮助您进一步了解JS中的GC主题:

我很确定我对所有这些链接并不详尽,但我想这是一个很好的起点,让您了解更多关于三个JS引擎的GC的所有细节!

相关问题