zoukankan      html  css  js  c++  java
  • underscore.js 源码阅读 一 整体结构

    // 整个underscore的实现包在一个立即执行函数中,避免污染全局对象
    // 通过call(this)来入全局变量
    (function() {
      // 缓存this
      var root = this;
      // 保存原始的“_”变量
      var previousUnderscore = root._;
    
      // Array,Object,Function这些都是函数,通过局部变量保存原型属性prototype也是为了方便压缩
      var ArrayProto = Array.prototype, ObjProto = Object.prototype;
      var SymbolProto = typeof Symbol !== 'undefined' ? Symbol.prototype : null;
    
      // 通过局部变量创建prototypes的快速索引,减少原型链查找
      var push = ArrayProto.push,
          slice = ArrayProto.slice,
          toString = ObjProto.toString,
          hasOwnProperty = ObjProto.hasOwnProperty;
    
      // 所有希望用到的ECMAScript 5原生的函数,在原生判断函数不存在的情况下,后面会进行重写
      var nativeIsArray = Array.isArray,
          nativeKeys = Object.keys,
          nativeCreate = Object.create;
    
      // 创建一个Ctor局部变量,避免后面每次创建function F(){}这个匿名函数
      var Ctor = function(){};
    
      // 定义`_`函数对象,用来生产`_`实例对象
      var _ = function(obj) {
        if (obj instanceof _) return obj;
        if (!(this instanceof _)) return new _(obj);
        this._wrapped = obj;
      };
    
      if (typeof exports != 'undefined' && !exports.nodeType) {
        if (typeof module != 'undefined' && !module.nodeType && module.exports) {
          exports = module.exports = _;
        }
        exports._ = _;
      } else {
        root._ = _;
      }
    
      // 版本号
      _.VERSION = '1.8.3';
      
      // 各种内部函数
      // ......
    
    
      // 各种公开的静态函数,例如:Collection Functions,Array Functions,Function (ahem) Functions等等
      // ......
    
      // 实例方法和属性
      _.prototype.value = function() {
        return this._wrapped;
      };
    
      // Provide unwrapping proxy for some methods used in engine operations
      // such as arithmetic and JSON stringification.
      _.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
    
      _.prototype.toString = function() {
        return String(this._wrapped);
      };
    
      // 支持AMD
      if (typeof define == 'function' && define.amd) {
        define('underscore', [], function() {
          return _;
        });
      }
    }.call(this));
  • 相关阅读:
    解决使用gomod后goland导包报红问题
    Golang写文件的坑
    Golang去除字符串前后空格
    Golang通过结构体解析和封装XML
    Golang获取CPU、内存、硬盘使用率
    Golang数组和切片的区别
    Golang修改操作系统时间
    Golang中GBK和UTF8编码格式互转
    Golang中的各种时间操作
    Golang十六进制字符串和byte数组互转
  • 原文地址:https://www.cnblogs.com/liuxiankun/p/6405552.html
Copyright © 2011-2022 走看看