javascript - underscore源码
天蓬老师
天蓬老师 2017-04-11 10:52:01
[JavaScript讨论组]
var optimizeCb = function (func, context, argCount) {
        if (context === void 0) return func
        switch (argCount == null ? 3 : argCount) {
        case 1: 
            return function (value) {
                return func.call(context, value)
            }
        case 2:
            return function (value, other) {
                return func.call(context, value, other)
            }
        case 3:
            return function (value, index, collection) {
                return func.call(context, value, index, collection)
            }
        case 4:
            return function (accumulator, value, index, collection) {
                return func.call(context, accumulator, value, index, collection)
            }
        }
        return function () {
            return func.apply(context, arguments)
        }
}

求教:上面是underscore的一段源码,让我比较困惑的是switch的作用,我觉得完全不需要switch啊,直接如果判断context==null,如果不是直接返回

return function () {
            return func.apply(context, arguments)
        }

上面这个不就可以吗?求各位大神指教啊?它的注释中说是

// Internal function that returns an efficient (for current engines) version
    // of the passed-in callback, to be repeatedly applied in other Underscore
    // functions.

请问是对于比较少参数的时候,用call这种方法会快不少吗?感觉有别的深意啊,毕竟我不觉得这个速度会差很多,可是实在想不到区别了

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号