underscore.js的一段核心函数,大神来分析下~

    // Internal function that returns an efficient (for current engines) version

    // of the passed-in callback, to be repeatedly applied in other Underscore

    // functions.

    var optimizeCb = function(func, context, argCount) {

        if (context === void 0) return func;

        switch (argCount) {

            case 1: return function(value) {

                return func.call(context, value);

            };

            // The 2-parameter case has been omitted only because no current consumers

            // made use of it.

            case null:

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

        };

    };


红糖糍粑
浏览 433回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript