JS模式--通用对象池的实现


        var objectPoolFactory = function (createObjFn) {
            var objectPool = [];
            return {
                create: function () {
                    var obj = objectPool.length === 0 ? createObjFn.apply(this, arguments) : objectPool.shift();
                    return obj;
                },
                recover: function (obj) {
                    objectPool.push(obj);
                }
            };
        };

        var iframeFactory = objectPoolFactory(function () {
            var iframe = document.createElement('iframe');
            document.body.appendChild(iframe);

            iframe.onload = function () {
                iframe.onload = null;
                iframeFactory.recover(iframe);
            };
            return iframe;
        });

        var iframe1 = iframeFactory.create();
        iframe1.src = 'http://www.baidu.com';

        var iframe2 = iframeFactory.create();
        iframe2.src = 'http://www.sina.com';

        setTimeout(function () {
            var iframe3 = iframeFactory.create();
            iframe3.src = 'http://www.qq.com';
        }, 10000);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM