Giter Site home page Giter Site logo

Comments (8)

WangBoxue avatar WangBoxue commented on July 29, 2024

仔细一想,如果已经实现了 _.difference,我们把 _.without 的参数放入数组,然后传入 _.difference 就 ok 了!倒过来就不行了(思考下为什么)。

感觉倒过来也可以

var args = _.union(arg1, arg2, ...);
args.unshift(array);
_.without.apply(null, args);

我哪里错了吗?

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024
_.without.apply(null, args);

这步的结果还是 args 吧?不知道我有没有理解错 @WangBoxue

from underscore-analysis.

WangBoxue avatar WangBoxue commented on July 29, 2024

@hanzichi
https://jsfiddle.net/WangBoxue/1z03b4h4/

apply() 方法在指定 this 值和参数(参数以数组或类数组对象的形式存在)的情况下调用某个函数。
所以执行_.without.apply(null, args)相当于_.without(array, elements in arg1, elements in arg2, ...)

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024

@WangBoxue 的确如此,感谢指出!

from underscore-analysis.

aleen42 avatar aleen42 commented on July 29, 2024

@WangBoxue @hanzichi

反过来实现可能需要通知 _.without() 是否是 _.difference() 来调用它。如果是,则展开参数中的所有数组,不然则不展开。当然,前提是 _.difference() 要保证调用 _.without() 时没有传递非数组元素。

_.without = function(array) {
    var rest = (this == 'difference') ? _.flatten(arguments, false, false, 1) : Array.prototype.slice.call(arugments, 1);
    
    return _.filter(array, function(value) {
        return !_.contains(rest, value);
    });
};

_.difference = function(array) {
    /** filter out arguments which is not an array firstly */
    var args = _.flatten(arguments, true, true, 1).unshift(array);

    /**
     * let the method `_.without()` know that whether it's called by `_.difference()`,
     * if so, then flatten all the arrays from argments,
     * if not, then do not flatten.
     */
    return _.without.apply('difference', args);
};

from underscore-analysis.

shaunzeng avatar shaunzeng commented on July 29, 2024

hey I have another question if you dont mind?

So in _.sample function, what is the point of having a Math.max(0,n) passed in _.shuffle function, instead of passing just 'n' ? also why does it check for object before using shuffle? because shuffle function already checks for objects? it that redundant ? please let me know what you think?

from underscore-analysis.

shaunzeng avatar shaunzeng commented on July 29, 2024

also, why does _.map function use cb(iteratee, context), instead of optimizeCb(iteratee, context) ? you cannot pass anything else but a function in _.map right? so it should be optimizeCb I think, i tried to pass other stuff but it kind of broke the _.map code. Please let me kow what you think? thanks a ton!

from underscore-analysis.

WangNianyi2001 avatar WangNianyi2001 commented on July 29, 2024

@shaunzeng Math.max 是过滤负数= =

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.