小白读lodash源码(一)未完结,迎接种种喷

1、 文件位置

lodash\dist\lodash.js

2、

;(function() {
}.call(this))   

这个函数的call要领的寄义:谁挪用它,this就指向当前的环境
 

call()要领在运用一个指定的this值和若干个指定的参数值的前提下挪用某个函数或要领。

 fun.call( thisArg[, arg1[, arg2[, ...]]])

thisArg在函数运行时指定的this值。指定的this值并不一定是该函数执行时真正的this值,假如这个函数处于非严厉形式下,则指定为ull何undefined的this值会自动指向全局函数(浏览器中就是window对象)。同时价为原始值(数字、字符串、布尔值)的this会指向该原始值的自动包装对象。

3、

line:400

  /* 
   * Detect free variable `global` from Node.js.
   * 从nodejs中发明自在变量global,就运用nodejs的global 
   */
  
  var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;

  /* 
   * Detect free variable `self`. 
   * 发明自在变量self,代表当前作用域
   */
   
  var freeSelf = typeof self == 'object' && self && self.Object === Object && self;

  /** Used as a reference to the global object. */
  var root = freeGlobal || freeSelf || Function('return this')();
  关于Function('return this')();的用法

4、 lodash和“_”怎样被划等号

 
 line:1405
 
 function runInContext(context) {}
 
 line:16705
 
   // Export lodash.

    var _ = runInContext();
    
    // Some AMD build optimizers, like r.js, check for condition patterns like:
      if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
    // Expose Lodash on the global object to prevent errors when Lodash is
    // loaded by a script tag in the presence of an AMD loader.
    // See http://requirejs.org/docs/errors.html#mismatch for more details.
    // Use `_.noConflict` to remove Lodash from the global object.
    root._ = _;

    // Define as an anonymous module so, through path mapping, it can be
    // referenced as the "underscore" module.
    define(function() {
      return _;
    });

 }
  // Check for `exports` after `define` in case a build optimizer adds it.
  else if (freeModule) {
// Export for Node.js.
(freeModule.exports = _)._ = _;
// Export for CommonJS support.
freeExports._ = _;
 }
  else {
// 赋值给全局对象。
root._ = _;
root.lodash = _;}

5、这里怎样将lodash imports _ 有何寄义?


 
    lodash.templateSettings=  {
    'escape': reEscape,
    'interpolate': reInterpolate,
    ‘variable’: '',
    'imports':{
        
        '_':lodash
    }
};

可以用以下要领挪用interpolate

[options.interpolate=_.templateSettings.interpolate]    

原型继续

    lodash.prototype = baseLodash.prototype;
    lodash.prototype.constructor = lodash;
    原文作者:aprildove
    原文地址: https://segmentfault.com/a/1190000006757741
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞