首页 > 其他 > 详细

一看就是好代码的代码(不断更新中)

时间:2015-09-02 19:22:29      阅读:241      评论:0      收藏:0      [点我收藏+]

缓存

        function repush(array, item) {

            // 对存在的item,更新到最后

            // 用ii加速for循环

            for (var i = 0, ii = array.length; i < ii; i++)

                if (array[i] === item) {

                    return array.push(array.splice(i, 1)[0]);

                }

        }


        function cacher(f, scope, postprocessor) {

            // f是生成缓存的函数

            // scope是f的域

            // postprocessor是处理旧缓存的函数

            // --------------------------------------

            // 1e3是lru队列的量,用短路方法(&&)执行

            function newf() {

                var arg = Array.prototype.slice.call(arguments, 0),

                    args = arg.join("\u2400"),

                    cache = newf.cache = newf.cache || {},

                    count = newf.count = newf.count || [];

                if (cache.hasOwnProperty(args)) {

                    repush(count, args);

                    return postprocessor ? postprocessor(cache[args]) : cache[args];

                }

                count.length >= 1e3 && delete cache[count.shift()];

                count.push(args);

                cache[args] = f.apply(scope, arg);

                return postprocessor ? postprocessor(cache[args]) : cache[args];

            }

            return newf;

        }

兼容amd cmd export

(function(root, factory) {

    if (typeof define === ‘function‘ && define.amd) {

        define([], factory);

    } else if (typeof exports === ‘object‘) {

        module.exports = factory();

    } else if (typeof define === "function" && define.cmd) {

        define(function(require, exports, module) {

            module.exports = factory();

        });

    } else {

        root.Sword = factory();

    }

}(this, function() {

}))

一看就是好代码的代码(不断更新中)

原文:http://my.oschina.net/u/1402334/blog/500810

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!