简体   繁体   中英

How 3rd arguments of Underscore.js _.each function work?

I'm learning Underscore.JS by follwing pluralsight course but i stuck at some points. Any help will be appreciate:

    var _ = require('underscore');
    var values = [{
            name: "Craig",
            state: "CA",
            price: 1
        }, {
            name: "John",
            state: "FL",
            price: 2
        }, {
            name: "Dan",
            state: "AZ",
            price: 8.5
        }, {
            name: "Elijah",
            state: "TN",
            price: 2.75
        }],

        evenCriteria = function(value) {
            return value.price % 2 === 0;
        },

        oddCriteria = function(value) {
            return value.price % 2 !== 0;
        },

        greaterThanEightCriteria = function(value) {
            return value.price > 8;
        },

        lessThanFiveCriteria = function(value) {
            return value.price < 5;
        };

    console.log('Even prices:');
    _.each(_.filter(values, evenCriteria), LogProperty, 'price');

    console.log('Odd prices:');
    _.each(_.filter(values, oddCriteria), LogProperty, 'price');

    console.log('Prices > 8:');
    _.each(_.filter(values, greaterThanEightCriteria), LogProperty, 'price');

    console.log('Prices < 5:');
    _.each(_.filter(values, lessThanFiveCriteria), LogProperty, 'price');

Since I can't find function LogProperty somewhere so I write my own:

        LogProperty = function (e, i, l) {
            console.log(e.price);
        }

But with the above function I wrote, it doesn't matter the 3rd argument is:

    _.each(_.filter(values, lessThanFiveCriteria), LogProperty, 'price');

print out the same value as:

    _.each(_.filter(values, lessThanFiveCriteria), LogProperty);

Can you give me some hints why it is like that? If I wrote the LogProperty as below, it return object instead:

        LogProperty = function (e, i, l) {
            console.log(e);
        }

To get the result, we need to pass the 3rd argument 'price' to this function. But from above code, 'price' is outside LogProperty function. From http://underscorejs.org/#each , I know that it has 3 arguments:

    _.each(list, iteratee, [context]) 

In this above example, does 'price' is the context? I don't think so since it has zero effect.

I think that the LogProperty function should have been written in another way that take the 3rd arguments in _.each function to pass in, by that way, I don't have to write exactly console.log(e.price);

Can you give me some hints so I can understand the code? Thanks!

The 3rd argument is [context] . That is, it defines the this value inside the function.

So if you did:

LogProperty = function(e, i, l) {
  console.log(this);
}

// this will be 'price'.
_.each(list, LogProperty, 'price');

you would get log 'price'.

You can also set this value for any function, using bind :

_.each(list, LogProperty.bind('price'));

is the same as above usage.

So in your case, you can solve your problem with something like:

LogProperty = function(e, i, l) {
  console.log(e[this]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM