【发布时间】:2022-06-28 22:48:59
【问题描述】:
我有一个函数返回true 或false,但我看到的是这个函数总是返回false。
const isLastImageAttachment = index => {
const isLastImage =
filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
return isLastImage ? true : false;
};
console.log('isLastImageAttachment()', isLastImageAttachment()); // false
isLastImage 返回true 或false 但isLastImageAttachment 总是返回false,即使isLastImage 是true。
这有什么问题?
尝试
我自己返回isLastImage。
const isLastImageAttachment = index => {
const isLastImage =
filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
return isLastImage;
};
但是isLastImageAttachment 返回false。
当然只回true,我有回true
const isLastImageAttachment = index => {
return true;
};
【问题讨论】:
-
不可能按现状说。这段代码无论如何都不会运行,因为你没有传入索引,所以
attachments[undefined]会抛出一个错误。另外,请注意isTrue ? true : false是多余的。你可以return isLastImage。 -
请注意,
return isLastImage ? true : false;与return isLastImage;完全相同,此外,我们不知道filteredImages或attachments是什么。 -
@AleksandrH:
attchments[undefined]不会扔,但undefined.uuid会。 -
console.log('isLastImageAttachment()', isLastImageAttachment()); // false你没有将index传递给函数...
标签: javascript reactjs