gpt4 book ai didi

javascript - 为什么 Object.keys(this).map(key => (this as any)[key])?

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:29 25 4
gpt4 key购买 nike

审查 this method我很好奇为什么它使用 Object.keys(this).map(key => (this as any)[key])? 只调用 Object.keys(this).indexOf(type) !== -1 是否同样有效:

/**
* Checks if validation type is valid.
*/
static isValid(type: string) {
return type !== "isValid" &&
type !== "getMessage" &&
Object.keys(this).map(key => (this as any)[key]).indexOf(type) !== -1;
}

最佳答案

该行不会创建对象键的数组,它会创建对象的 数组,并检查 type 是否包含在大批。如果是就更清楚了

/**
* Checks if validation type is valid.
*/
static isValid(type: string) {
return type !== "isValid" &&
type !== "getMessage" &&
Object.values(this).includes(type);
}

(当然包括必要的 polyfill)

关于javascript - 为什么 Object.keys(this).map(key => (this as any)[key])?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50808617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com