我试图得到callback functions
的挂钩,但我遇到了一些我不完全了解的东西。.filter()如何使回调函数调用另一个函数?
这段代码在下面的例子中使用时效果很好。
zombieCreatures = creatures.filter(filterCreatures);
不幸的是,这个代码块在同一个例子中不起作用。
zombieCreatures = creatures.filter(function(v) {
filterCreatures(v);
});
对我来说,他们看起来像相同的指令,但显然我不正确。有什么不同? .filter()
之内是否有可能使回调函数调用另一个函数?
'use strict';
var creatures = [], zombieCreatures = [];
var filterCreatures;
creatures = [
{species: 'Zombie', hitPoints: 90},
{species: 'Orc', hitPoints: 40},
{species: 'Skeleton', hitPoints: 15},
{species: 'Zombie', hitPoints: 85}
];
\t
filterCreatures = function(v) {
return v.species === 'Zombie';
}
\t
zombieCreatures = creatures.filter(function(v) {
filterCreatures(v);
});
console.log(zombieCreatures);
非常感谢你,我拉着我的头发!我从来没有想过使用'return'。我要阅读'return'。我把那一个拧得比我想要承认的要多。 – DR01D