我收到此警告:
Unexpected assignment expression.
return task.completed = true; // Line 63, Pos 39
使用此代码时:
completeAll: function () {
this.tasks = this.tasks.filter(function (task) {
return task.completed = true;
});
}
为什么?而且我还能如何编写该表达式以避免JSLint引发警告?
附言
The codeblock is taken from the Vue TodoMVC Example here: http://todomvc.dev/examples/vue/, therefore I assume that code review must have already been happened.
It's doing that because it's warning you that you're using
=
rather than==
or===
in a context where you're not just assigning, but also doing something with the assigned result. That's perfectly valid JavaScript, but it's frequently unintentional. A better example is:...where you probably meant
==
or===
(checking that they were equal).How you fix it depends on what you're trying to do. From the name of the method, I assume you're (well, they're) trying to set
task.completed
, in which case franklyfilter
is the wrong function to use; they should be usingforEach
:but if you (they) really wanted to use
filter
:如果您要进行比较(我对此表示怀疑),而不是进行分配,那么:
要么
要么