Source code analysis of v-1.8.2 under core

Keywords: Attribute Javascript

V-1.8.2 source code analysis of anderscore (2)

if(typeof exports !== "undefined") {
        if(typeof module !== "undefined" && module.exports) {
            exports = module.exports = _;
        }
        exports._ = _;
    } else {
        root._ = _;
    }

Assign the local variable defined above to the property in the global object
That is, in the client, window_
exports in the server (node)_
On node.js and browser client, it is a global variable

_.VERSION = "1.8.2";

Current version

    var optimizeCb = function(func, context, argCount) {
        // If there is no this point, the original function will be returned
        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);
                };
            case 3:
                return function(value, index, collection) {
                    return func.call(context, value, index, collection);
                };
            case 4:
                return function(value) {
                    return func.call(context, value, index, collection);
                };
        }
        return function() {
            return func.apply(context, arguments);
        };
    };

Void operator can evaluate the given expression, and then return undefined and save bytes. Many js tools use void 0 instead of undefined in the compression process.
The optimization of callback function is mainly used to change the execution environment and function of the function, handle different parameters, optimize js engine and avoid using arguments
Use the call and apply functions to bind the execution context.
Return different functions or values according to whether there are parameters and different types of parameters

var cb = function(value, context, argCount) {
// According to different value attributes, return calls different functions
    if(value == null) return _.identity;
    if(_.isFunction(value)) return optimizeCb(value, context, argCount);
    if(_.isObject(value)) return _.matcher(value);
        return _.property(value);
    };
    _.iteratee = function(value, context) {
        return cb(value, context, Infinity);
    };
    var createAssigner = function(keysFunc, undefinedOnly) {
        return function(obj) {
            var length = arguments.length;
            // When the number of returned function parameters is 0 or 1 or null
            if(length < 2 || obj == null) return obj;
            // When the number of returned function parameters is greater than 1
            for(var index = 1; index < length; index++) {
            // var a variable source receives arguments [1], and source is the object parameter;
                var source = arguments[index],
            // Pass the key value pair of the second starting object parameter as the parameter to the first parameter function
            // Returns an array
                    keys = keysFunc(source),
                    l = keys.length;
                for(var i = 0; i < l; i++) {
                    var key = keys[i];
            // Only when obj[key] is undefined can it be overwritten, that is, if there is the same key value, then
            // Take the value of the first occurrence
                    if(!undefinedOnly || obj[key] === void 0)
                        obj[key] = source[key];
                }

            }
            return obj;
        };
    };
// An internal function for creating a new object that inherits from another.
// Create a new object that inherits from another object, prototype inherits
var baseCreate = function(prototype) {
    // If prototype is not an object, a new object is returned
        if(!_.isObject(prototype)) return {};
    // A new object creation method of es5. If the browser supports this property, perform this step to create a new object
        if(nativeCreate) return nativeCreate(prototype);
        Ctor.prototype = prototype;
        var result = new Ctor;
        Ctor.prototype = null;
        return result;
    // Return a function instance
    };
var property = function(key) {
        return function(obj) {
            return obj == null ? void 0 : obj[key];
        };
    };
//Closure
 //Returns a function. If obj is null or undefined, it returns undefined. Otherwise, obj[key] is returned
 //Property value corresponding to key object
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
    // Returns the 53rd power of 2 - 1
var isArrayLike = function(collection) {
        var length = collection != null && collection.length;
// Get the length of the parameter, judge that the class array has the length attribute, and limit the length range
        return typeof length == "number" && length >= 0 && length <= MAX_ARRAY_INDEX;
// Using short circuit to optimize performance, return the type of length as a number, and the length is between 0-2 ^ 53-1
    };
// isArrayLike determines whether collection is an array or object of class
// Helper for collection methods to determine whether a collection
// should be iterated as an array or as an object
// Math.pow(2, 53) - 1 is the largest number that can be accurately represented in JavaScript

Posted by crackfox on Thu, 30 Apr 2020 11:59:38 -0700