Backbone.js - Todo Collection - 这个返回语句中到底发生了什么?

发布于 2025-01-02 22:34:00 字数 949 浏览 0 评论 0原文

我正在回顾 Backbone 待办事项列表,并对集合有疑问。

这是代码:

window.TodoList = Bacbone.Collection.extend({

        model: Todo,

        localStorage: new Store("todos"),

        done: function() {
            return this.filter(function(todo){return todo.get("done")})
        },

        remaining: function() {
            return this.without.apply(this, this.done());
        }



    })

我了解这里发生的一切,除了“剩余”功能。

return 语句: return this.without.apply(this, this.done()); 正在使用下划线方法的代理 - _.without

根据 Underscore 文档,这就是它的用途:

without_.without(array, [*values]) 返回数组的副本 已删除值的所有实例。 === 用于相等 测试。

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); => [2,3,4]

所以,我明白它是说返回集合中的所有内容,而不包含值为“true”的“done”属性。

我不明白的是链接到它的“应用”功能。这不会出现在 Backbone 文档或 Underscore 文档中。至少我在任何地方都找不到它。

谁能详细解释一下 Return 语句中这些元素发生了什么?

I'm looking back at the Backbone todo list and have a question about the collection.

Here is the code:

window.TodoList = Bacbone.Collection.extend({

        model: Todo,

        localStorage: new Store("todos"),

        done: function() {
            return this.filter(function(todo){return todo.get("done")})
        },

        remaining: function() {
            return this.without.apply(this, this.done());
        }



    })

I understand everything that is going on here, except for the 'remaining' function.

The return statement: return this.without.apply(this, this.done()); is using a proxy to an underscore method - _.without

According to Underscore docs, here is what that is for:

without_.without(array, [*values]) Returns a copy of the array with
all instances of the values removed. === is used for the equality
test.

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2, 3, 4]

So, I get that it is saying to return everything in the collection without a 'done' attribute with the value of 'true'.

What I don't understand is the 'apply' function that is being chained to it. That doesn't appear in the Backbone docs or the Underscore docs. At least not anywhere I can find it.

Can anyone explain in detail what is going on with those elements in the Return statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

南街九尾狐 2025-01-09 22:34:00

this 指的是集合。

apply 是 JavaScript 函数的一种方法,允许您设置方法的上下文并将值数组发送给调用者。

apply 期望上下文作为第一个参数,然后是一个数组类似数组(例如参数),这将是作为参数传入函数。

您可以使用 .call 执行相同的操作,但第二个以上参数以逗号分隔。

applycall 是 JavaScript 原生的。

所以...

return this.without.apply(this, this.done());

方法 this.done() 返回一个数组,但使用上下文集合并通过 without 方法传入一系列要忽略的值。这又返回集合中未完成的所有待办事项。

例子:

_.without([1,2,3,4],1,2); === _.without.call([], [1,2,3,4], 1, 2);

this is referring to the collection.

apply is a method of javascript functions that allows you to set context of a method and send an array of values to the caller.

apply expects context as the first parameter then an array or array-like (such as arguments) which will be passed in as parameters the function.

You can do the same thing with .call except the 2nd+ params are comma separated.

apply and call are native to javascript.

So...

return this.without.apply(this, this.done());

the method this.done() returns an array, but uses the context of the collection and passes in a series of values to be ignored via the without method. Which in turn returns all todos that aren't done within the collection.

Example:

_.without([1,2,3,4],1,2); === _.without.call([], [1,2,3,4], 1, 2);
风尘浪孓 2025-01-09 22:34:00

我的理解是,在这种情况下,使用 apply 是多余的,remaining 可以缩短如下:

remaining: function() {
  return this.without(this.done());
}

据我了解,使用 的唯一原因apply 表示您想要(或需要)更改 without 将操作的上下文项。在这种情况下,我们没有必要这样做。

如果我错了,我真的(真的!)想要解释为什么这里需要 apply

My understanding is that, in this case, the use of apply is redundant, remaining could be shortened as follows:

remaining: function() {
  return this.without(this.done());
}

As I understand it, the only reason to use apply is if you want (or need) to change the contextual item that without will operate on. In this case, we have no need to do that.

If I'm wrong, I'd really (really!) like to have an explanation of why apply is necessary here.

妄断弥空 2025-01-09 22:34:00

apply 调用一个函数,并将该函数上下文中的 this 绑定到传递的第一个参数(在本例中为 Collection 实例 TodoList)。第二个参数是要传递给 without 的参数数组。

顺便说一下,apply 不是 Backbone 的东西——它是 JavaScript 原生的。

apply invokes a function and binds this in the context of that function to the first argument passed (in this case, the Collection instance TodoList). The second argument is an array of arguments to be passed to without.

By the way, apply isn't a Backbone thing -- it's native to JavaScript.

我一直都在从未离去 2025-01-09 22:34:00

的原因

this.without.apply(this, this.done())

是“_.without”不接受作为单个数组排除的项目数组作为参数 ([ ]) 参数

请参阅此处_.不接受数组作为第二个参数

应用< /em>,这是 JS 的一部分Function.prototype,这是在单个数组参数中注入排除项的解决方法

The reason for this

this.without.apply(this, this.done())

is that "_.without" does not accept as argument the array of items to be excluded as a single array ([]) argument

See here _.without to accept array as second argument

apply, which is part of the JS Function.prototype, here is a workaround to inject the excluding items in a single array argument

智商已欠费 2025-01-09 22:34:00

在这种情况下使用 apply 是多余的,因为骨干集合正在正确地完成工作(参见)。 http://backbonejs.org/docs/backbone.html#section-122

我们可以像这样使用下划线: _.without.apply(this, this.done()) 或像这样使用主干绑定: this.without(this.done) 通过使用主干绑定。

the use of apply in this case is redundant because backbone collections is doing the job correctly cf. http://backbonejs.org/docs/backbone.html#section-122

we can use underscore like this: _.without.apply(this, this.done()) or backbone binding like this: this.without(this.done) by using backbone bind.

望她远 2025-01-09 22:34:00

请看一下下划线文档:
像这样:

without_.without(array, [*values])
返回数组的副本,其中删除了值的所有实例。

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2,3,4]

Please take a look at underscore doc :
like this :

without_.without(array, [*values])
Returns a copy of the array with all instances of the values removed.

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2, 3, 4]

翻了热茶 2025-01-09 22:34:00

我将函数更改为这个,并在 TODO 列表应用程序上得到了完全相同的结果:

remaining: function () {
        return this.filter(function (todo) {
            return !todo.get('done');
        });
}

我仍然不明白 apply 如何成为 without 的方法,我知道 apply 是一个 Javascript 函数,但后来我读了 apply 文档 并理解在这种情况下没有被用于面向对象的方式(参见http://underscorejs.org/#chain)。

事实上,apply 可以传递 null 而不是 this 作为上下文,并且它不会改变结果,因为它根本没有被使用:

return this.without.apply(null, this.done());

注意第一个this是模型的实际集合,without,通过apply中的第二个参数,这是done的数组(已完成) 任务,正在生成待办事项数组。

顺便说一句,最新版本的 TODO 应用程序将函数 done 重命名为 completed

I changed the function to this and got the exact same result on the TODO list application:

remaining: function () {
        return this.filter(function (todo) {
            return !todo.get('done');
        });
}

I still didn't understand how apply became a method of without, I knew apply was a Javascript function, but then I read the documentation for apply and understood that in this case without was being used in an object-oriented way (see http://underscorejs.org/#chain).

In fact, apply could be passed null instead of this as the context and it wouldn't change the result because it's not being used at all:

return this.without.apply(null, this.done());

Notice the first this is the actual collection of models and without, via the second argument in apply, which is the array of done (completed) tasks, is producing the array of pending todo tasks.

By the way, the latest version of the TODO application renames the function done to completed.

梦醒时光 2025-01-09 22:34:00

这里 this.without() 委托给 _.without() 函数。 _.without() 需要一个数组和元素作为参数而不是数组。通过使用 apply() , apply 以正确的方式调用 _.without() 。

var obj = {
    f1: function(a,b,c){}
};

Now obj.f1(1,2,3) == obj.f1.apply(obj, [1,2,3]).

有了这些信息, this.without(this.complete()) 将一个数组传递给 without 方法。但如果没有方法,则需要将单个元素作为参数传递。这可以使用 Function.apply() 来完成。

Here this.without() delegates to _.without() function. _.without() needs an array and elements as parameters not as an array. By using apply() , apply calls _.without() in the correct manner.

var obj = {
    f1: function(a,b,c){}
};

Now obj.f1(1,2,3) == obj.f1.apply(obj, [1,2,3]).

With this infomation, this.without(this.complete()) passes an array to the without method. But without method needs individual elements to be passed as arguments. That can be done using Function.apply().

絕版丫頭 2025-01-09 22:34:00

without 函数需要一个要从 this 中删除的元素列表。
this.completed() 返回一个数组,因此它不是 without 函数所期望的。

apply 是一个原生 JavaScript 函数,它调用一个函数,将 this 上下文设置为第一个参数,将数组设置为第二个参数。该参数作为其列表参数传递给原始函数。

在本例中,apply 将参数传递给 this.completed() 中的 without,满足 without 的期望。

总之,在这种情况下,apply是必要的。

The without function needs a list of elements to remove from this.
this.completed() returns an array, therefore it is not what without function is expecting.

apply is a native JavaScript function, which calls a function setting the this context as the first argument and an array as the second argument. The argument is passed to the original function as its list arguments.

In this case apply passes the arguments to without in this.completed(), meeting the expectation of without.

In conclusion, in this case, apply is necessary.

我一向站在原地 2025-01-09 22:34:00

抱歉,我是这个东西的新手,但不能将 fn.remaining (也)声明为:

return this.filter(function(todo){return !todo.get("done")})

说明这一点作为澄清请求,而不是替代声明:)

(编辑:无法在“todo.get...”之前加粗“!”)

Sorry, I'm a total newb @ this stuff, but couldn't fn.remaining (also) be declared as:

return this.filter(function(todo){return !todo.get("done")})

Stating this as a request for clarification, rather than an alternative declaration :)

(edit: couldn't bold the '!' before 'todo.get...')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文