Code Monkey home page Code Monkey logo

Comments (15)

Shepherdog avatar Shepherdog commented on June 5, 2024

楼主辛苦了,受益匪浅,帮我对Lodash的理解又进了一步

from underscore-analysis.

lessfish avatar lessfish commented on June 5, 2024

@Shepherdog 楼主有时间也看看 Lodash,哈哈

from underscore-analysis.

kongzhexin avatar kongzhexin commented on June 5, 2024

对于 ([1, 2, 3]).chain() 为什么可以执行思考了半天 ,chain函数没有传参 但实际通过result(this, func.apply(, args)) 已经传递过去了。 感觉自己的逻辑思维或者思考方式是否不对,对于这种函数总是理解起来很困难, 楼主,可否指点一二。

from underscore-analysis.

lessfish avatar lessfish commented on June 5, 2024

并不是 ([1, 2, 3]).chain() 吧,应该是 _([1, 2, 3]).chain() 吧?@kongzhexin

from underscore-analysis.

alsotang avatar alsotang commented on June 5, 2024

from underscore-analysis.

kongzhexin avatar kongzhexin commented on June 5, 2024

初次使用github 评论 ,打出来正确 显示有问题 ,其实我想问的是思路 @hanzichi

from underscore-analysis.

lessfish avatar lessfish commented on June 5, 2024

@kongzhexin 没看懂你的意思。_ 看做构造方法,然后 _([1, 2, 3]) 返回构造对象,对象的原型链上有 chain 这个方法,这不就能执行了?

from underscore-analysis.

wangxx1991 avatar wangxx1991 commented on June 5, 2024

还在学习中,谢谢楼主。

from underscore-analysis.

pageliu16 avatar pageliu16 commented on June 5, 2024

感谢,现在正在研究中。

from underscore-analysis.

rookiePrgrmer avatar rookiePrgrmer commented on June 5, 2024

@hanzichi 请教楼主,下面的代码有什么作用?百思不得其解,难道method.apply还不够吗?

if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];

from underscore-analysis.

rookiePrgrmer avatar rookiePrgrmer commented on June 5, 2024

@kongzhexin 时间过去很久了,不知道你有没有弄懂。

_([1, 2, 3]).chain()

这行代码分两部分,第一个部分_([1, 2, 3])你应该知道,就是通过var _ = function() {}构造函数创建了一个对象,然后把[1, 2, 3]包装到了里面,对应的是_wrapped字段,最终返回了这个新建的对象。
第二部分实际就是调用了上面创建的对象的chain实例方法。chain方法是通过mixin添加到了var _ = function() {}的原型中,因此通过OOP的方式执行chain的时候,实际执行的是以下代码:
.prototype[name] = function() { // 参考.mixin的实现
// 这里的this就是通过underscore构造函数创建的对象,
// 它的_wrapped属性指向的就是源对象,或者称为被包装对象
var args = [this.wrapped];
// 拼接为完整参数
push.apply(args, arguments);
return result(this, func.apply(
, args));
};
那么这里关键就是func.apply,实际执行的又是_.chain()了。在执行_.chain的时候,传递的第一个也是唯一一个参数就是args中的第一个元素,也就是被包装的那么源对象,那么这里就是[1, 2, 3]了。
后面的过程就是常规执行_.chain()了,你应该懂的。

from underscore-analysis.

rookiePrgrmer avatar rookiePrgrmer commented on June 5, 2024

@hanzichi 还有一点,result方法里面直接_.chain(obj)不行吗?为什么一定要_(obj).chain()感觉饶了一圈啊?我试了下好像也没啥问题。

from underscore-analysis.

ZeaLot4J avatar ZeaLot4J commented on June 5, 2024

@rookiePrgrmer if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
是为了兼容IE毒瘤。

from underscore-analysis.

aswind7 avatar aswind7 commented on June 5, 2024

为什么 不能去掉 result函数, 并将

// 执行 func 方法
// 支持链式操作
return result(this, func.apply(_, args));

改为 以下这样呢?(这样更省事,少了一个函数,如果有_chain则直接修改_wrapped,返回原有实例)

				let result = func.apply(this, args);
				if (this._chain) {
					this._wrapped = result;
					result = this;
				}
				return result;

因为 这样会有风险 导致原有的实例属性被更改, 比如 以下代码:


var a = _('abc').chain();
var b = a.toUpperCase();

//此时 a 和b 的 _wrapped都会变为  "ABC" 

from underscore-analysis.

shangguanhonglei avatar shangguanhonglei commented on June 5, 2024

多谢楼主分享,感谢

from underscore-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.