1 回答

TA貢獻1796條經(jīng)驗 獲得超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');
那么如何解決呢?最有可能的最簡單方法是創(chuàng)建一個名為 的巨型配置對象characters,其中包含每個角色名稱的屬性,其中包含一個具有所有屬性的對象。通過使用對象,您可以通過字符串引用字符來獲取具有所有屬性的對象:
顯示整個對象,然后是各個統(tǒng)計信息/屬性:
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']);
添加回答
舉報