如何在 map 函数中使用简单的 JavaScript查找表(即映射本身)?即我怎样才能摆脱这个字段(从这里"code"
借来的),并且只使用方法内部的查找表?map
const Resistors = {
"black": 0, "brown": 1, "red": 2,
"orange": 3, "yellow": 4, "green": 5,
"blue": 6, "violet": 7, "grey": 8,
"white": 9,
// Why not Resistors[color]
"code" : (color: string) => {
function valueOf<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
return valueOf(Resistors, color as any);
}
}
class ResistorColor {
private colors: string[];
constructor(colors: string[]) { this.colors = colors; }
value = () => {
return Number.parseInt(
this.colors
.map(Resistors.code) // How can i get rid of .code here?
.join("")
)
}
}
Helenr
慕盖茨4494581
相关分类