我正在尝试编写一些从对象加载文本并以持续存在的方式随机分配性别的东西
const output = {
text: "He thought that his sweater would suit him"
}
我希望能够让该对象在显示时随机显示“他认为他的毛衣会适合他”或“她认为她的毛衣会适合她”。在尝试解决这个问题而不必本质上创建两个不同的对象——一个用于男性,一个用于女性——我已经做到了这一点:
const gender = {
male: {
pronoun: "he",
possPronoun: "his",
possAdjective: "his",
object: "him",
moniker: "sir"
},
female: {
pronoun: "she",
possPronoun: "hers",
possAdjective: "her",
object: "her",
moniker: "ma'am"
}
};
function randGender (usage) { // Add lettercase functionality later
return Math.floor(Math.random()*2) == 1 ? gender.female[usage] : gender.male[usage]
}
console.log(randGender("pronoun"));
我被卡住的地方是在此之后要做什么。
const output = {
text: `${randGender("pronoun")} thought that ${randGender("possAdjective")} sweater would suit ${randGender("object")}`
}
console.log(output.text); //she thought that his sweater would suit her
...当我希望它保持所有一种性别时,显然不起作用。
我认为有一些解决方法,例如使用 output.text.male 和 output.text.female 并在调用对象时执行随机部分。有什么方法可以按照我的方式进行吗?
RISEBY
胡子哥哥
相关分类