Вы очень близки к тому, чтобы получить это правильно. Я считаю, что проблема, с которой вы столкнулись, заключается в том, что вы ожидаете, что строка "jin"
будет ссылаться на ваш const
jin
. Однако механизм рендеринга JS на самом деле работает не так - строка "jin"
передается просто как строка, поэтому все ваши значения отображаются undefined - потому что строка "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']);