简体   繁体   English

Underscore.js _.each函数的第三个参数如何工作?

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

I'm learning Underscore.JS by follwing pluralsight course but i stuck at some points. 我正在通过遵循多元观察课程来学习Underscore.JS,但我在某些时候仍然坚持。 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函数,因此我编写了自己的函数:

        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,它将返回object:

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

To get the result, we need to pass the 3rd argument 'price' to this function. 为了获得结果,我们需要将第三个参数“ price”传递给该函数。 But from above code, 'price' is outside LogProperty function. 但是从上面的代码来看,“价格”不在LogProperty函数的范围内。 From http://underscorejs.org/#each , I know that it has 3 arguments: http://underscorejs.org/#each ,我知道它有3个参数:

    _.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); 我认为LogProperty功能应该已经写在另一种方式是从第三个论点_.each函数传递,通过这种方式,我没有写准确的console.log(e.price);

Can you give me some hints so I can understand the code? 您能给我一些提示以便我理解代码吗? Thanks! 谢谢!

The 3rd argument is [context] . 第三个参数是[context] That is, it defines the this value inside the function. 也就是说,它在函数内部定义了this值。

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 : 您还可以使用bind为任何函数设置this值:

_.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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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