Giter Site home page Giter Site logo

Comments (8)

changxiupeng avatar changxiupeng commented on September 3, 2024 14

@zhoucumt
cb 会检查 iteratee 是否为函数,只有当 iteratee 是函数时,才会在 cb 内部调用 optimizeCb 对 iteratee 进行优化(optimizeCb 只能优化函数)。
因为不像 each 那样 iteratee 肯定是函数,map 中 iteratee 可以是对象或字符串等:
var results = _.map([{name:'cxp'},{name:'comma'}],'name'); // => results: ['cxp', 'comma'];
如果 iteratee 不是函数,cb 就不会调用 optimizeCb, 而是返回其他函数对 map 中传入的集合进行迭代。

from underscore-analysis.

zhoucumt avatar zhoucumt commented on September 3, 2024

再请教一个问题,.each和.map中,为什么前者的迭代函数是iteratee = optimizeCb(iteratee, context);而后者的却是iteratee = cb(iteratee, context);

from underscore-analysis.

lessfish avatar lessfish commented on September 3, 2024

@zhoucumt

好问题,这点我也十分诧异,个人觉得两者作用相同,可以互换。类似的还有 _.each 源码中用了 if else 结构,而 _.map 中没有用,我觉得也是一样的。唯一可以想到的原因是,可能为了测试 optimizeCbcb 两个内部方法的正确性?

from underscore-analysis.

aleen42 avatar aleen42 commented on September 3, 2024

可否采用 undefinedOnly === void 0 来判断,而非 arguments.length

from underscore-analysis.

ooooevan avatar ooooevan commented on September 3, 2024

如果在严格模式,这里用的arguments还行吗?

from underscore-analysis.

aswind7 avatar aswind7 commented on September 3, 2024

@ooooevan 不行。

from underscore-analysis.

wangliang1124 avatar wangliang1124 commented on September 3, 2024

@sqfbeijing 为什么不行,可以的啊,严格模式只是淘汰了arguments.callee 和 arguments.caller

from underscore-analysis.

shenzhim avatar shenzhim commented on September 3, 2024

@anotherleon caller 并不在 arguments对象上的

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.