码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-03-03 17:13:41      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:ret   tee   document   pen   recover   load   .com   code   通用   

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

 

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

标签:ret   tee   document   pen   recover   load   .com   code   通用   

原文地址:http://www.cnblogs.com/meiyh/p/6497552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!