简体   繁体   English

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

[英]lifecycle of an object returned from a function

When using a module-esq pattern to allow chaining of methods, how long do the returned objects last before being garbage collected? 当使用module-esq模式来允许方法链接时,返回的对象在被垃圾回收之前持续多长时间? I really like the way jquery allows chaining methods but I'm concerned it will pollute the memory of the page with lots of unnecessary objects if I use th pattern for my code. 我真的很喜欢jquery允许链接方法的方式,但是我担心如果我的代码使用模式会污染很多不必要对象的页面内存。

here is a simple example 这是一个简单的例子

(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');

Here is more real world example: 这是更多真实的示例:

(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;

})();

to access the methods from the above code you would use something like 从上面的代码访问方法,您将使用类似

get('elemId').css(('prop': 'value'}).current().css('prop');

thanks for any answers. 感谢您的任何答案。

Cheers, 干杯,

Andrew 安德鲁

Well, first of all you should read this question: Learning garbage collection theory 好吧,首先,您应该阅读以下问题: 学习垃圾收集理论

Then, you need to know that every javascript runtime implement its own very specific GC, so there is absolutely no rule to when and how the objects are garbage collected. 然后,您需要知道每个javascript运行时都实现自己的非常特定的GC,因此对于何时以及如何对对象进行垃圾回收绝对没有规则 Once they are not referenced you should consider them gone forever, and trust (I know, it's a lot to ask) that GC will release the memory at the "best time" after the object are dereferenced. 一旦不引用它们,您应该认为它们已经永远消失了,并且相信(我知道,要问很多),GC将在取消引用对象后的“最佳时间”释放内存。

To know more about specific stuff about each GC, you need to go read resources concerning each one of them, luckily for you, there is now only 3 major engines! 要了解有关每个GC的特定内容的更多信息,您需要阅读有关每个GC的资源,幸运的是,现在只有3个主要引擎! Here are resources to help you get further on the topic of GC in JS: 以下资源可帮助您进一步了解JS中的GC主题:

I'm pretty sure I'm not exhaustive with all those links, but I guess it's a good starting point for you to learn more about all the specifics on the GC of the three JS engines! 我敢肯定,我并没有穷尽所有这些链接,但是我想这是一个很好的起点,让您了解更多有关三个JS引擎GC的所有详细信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM