1 回答
TA贡献1796条经验 获得超4个赞
你非常接近这个正确。我相信您面临的问题是您希望字符串 of"jin"引用您的const jin. 然而,这并不是 JS 渲染引擎的工作方式——字符串 of"jin"只是作为字符串传递,这就是为什么你的所有值都显示为未定义——因为字符串"jin"没有你正在寻找的任何属性。
这将记录传递的字符串"jin",然后是几个undefined:
const jin = {
// Profile
name: "Jin Kazama",
nickname: "The Child of Destiny",
flag: "../img/flagicons/japan.svg",
image: "../img/characters/jin.png",
age: 21,
country: "Japan",
fightingStyle: "Traditional karate",
debut: "<em>Tekken 3</em>",
// Scores
offense: 9,
defence: 10,
range: 8,
punishment: 8,
gimmicks: 3,
execution: 3,
hurtbox: 3,
// Playstyle
playstyle: "Versatile, keep-out, Mishima",
introduction: "<p>Versatile character who performs at his best in the mid-range, armed with good poking, great counter hit tools, great damage output, variety in his throws and a unique parry that deals with all highs and mids except projectiles (fireballs). While his Mishima-style tools are not quite as effective as those of the out-and-out Mishima characters, he makes up for it with other situational moves that plug those weaknesses. He does, however, lack range on a few key punishers.</p>",
};
const display = character => {
console.log(character);
console.log(character.name);
console.log(character.nickname);
console.log(character.flag);
console.log(character.image);
console.log(character.age);
console.log(character.country);
console.log(character.fightingStyle);
console.log(character.debut);
console.log(character.offense);
console.log(character.defence);
console.log(character.range);
console.log(character.punishment);
console.log(character.gimmicks);
console.log(character.execution);
console.log(character.hurtbox);
console.log(character.playstyle);
console.log(character.introduction);
}
display('jin');
那么如何解决呢?最有可能的最简单方法是创建一个名为 的巨型配置对象characters,其中包含每个角色名称的属性,其中包含一个具有所有属性的对象。通过使用对象,您可以通过字符串引用字符来获取具有所有属性的对象:
显示整个对象,然后是各个统计信息/属性:
const characters ={
jin: {
// Profile
name: "Jin Kazama",
nickname: "The Child of Destiny",
flag: "../img/flagicons/japan.svg",
image: "../img/characters/jin.png",
age: 21,
country: "Japan",
fightingStyle: "Traditional karate",
debut: "<em>Tekken 3</em>",
// Scores
offense: 9,
defence: 10,
range: 8,
punishment: 8,
gimmicks: 3,
execution: 3,
hurtbox: 3,
// Playstyle
playstyle: "Versatile, keep-out, Mishima",
introduction: "<p>Versatile character who performs at his best in the mid-range, armed with good poking, great counter hit tools, great damage output, variety in his throws and a unique parry that deals with all highs and mids except projectiles (fireballs). While his Mishima-style tools are not quite as effective as those of the out-and-out Mishima characters, he makes up for it with other situational moves that plug those weaknesses. He does, however, lack range on a few key punishers.</p>",
}
};
const display = character => {
console.log(character);
console.log(character.name);
console.log(character.nickname);
console.log(character.flag);
console.log(character.image);
console.log(character.age);
console.log(character.country);
console.log(character.fightingStyle);
console.log(character.debut);
console.log(character.offense);
console.log(character.defence);
console.log(character.range);
console.log(character.punishment);
console.log(character.gimmicks);
console.log(character.execution);
console.log(character.hurtbox);
console.log(character.playstyle);
console.log(character.introduction);
}
display(characters['jin']);
添加回答
举报