Javascript数组原型查找

Javascript数组原型查找,javascript,arrays,find,prototype,Javascript,Arrays,Find,Prototype,在developer.mozilla上,我找到了一个使用数组查找的示例: const inventory = [ {name: 'apples', quantity: 2}, {name: 'bananas', quantity: 0}, {name: 'cherries', quantity: 5} ]; function isCherries(fruit) { return fruit.name === 'cherries'; } console.log(inventor

在developer.mozilla上,我找到了一个使用数组查找的示例:

const inventory = [
  {name: 'apples', quantity: 2},
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
];

function isCherries(fruit) {
  return fruit.name === 'cherries';
}

console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }
我有一个cart类和一个对象数组,因此,我有一个函数checkQuantity,它应该返回满足条件的任何项目。我也有同样的功能,我需要找到

我尝试从mozilla实现这种方法,我喜欢这样:

itemSearch(item) {
        return item.id === this.id &&
            item.color === this.color &&
            item.size === this.size
    } // method which i need
 checkQuantity() {
        return this._cart.find(this.itemSearch()).quantity < this.stockCount();
    }
我是这样使用它的:

itemSearch(item) {
        return item.id === this.id &&
            item.color === this.color &&
            item.size === this.size
    } // method which i need
 checkQuantity() {
        return this._cart.find(this.itemSearch()).quantity < this.stockCount();
    }
checkQuantity(){
返回此._cart.find(this.itemSearch()).quantity
在这里,我获得undefined,但我肯定它必须找到,因为如果我使用.find(element=>conditions)而不是该方法,它会工作


所以,我的问题是为什么它不起作用?抱歉,英语不好。

使用
this
,除了不使用函数调用的结果外,还需要指定
this

checkQuantity() {
    return this._cart.find(this.itemSearch, this).quantity < this.stockCount();
}
checkQuantity(){
返回此._cart.find(this.itemSearch,this).quantity
您是否有一个包含类和(非)工作示例的数据集?再看看您从中获得灵感的示例。它们确实是
.find(isCherries)
,而不是
.find(isCherries())
@blex,我的第一次尝试没有(),它无论如何都不起作用。@如果是WMZ,你必须显式地绑定到
这个
这个。_cart.find(this.itemSearch.bind(this))
。否则您将丢失正确的上下文,
将指向
窗口
。非常感谢你。等待10分钟,我标记为已回答!