小白读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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值