Javascript 中性别的动态对象值
Dynamic object value for gender in Javascript
我正在尝试编写从对象加载文本并以持续存在的方式随机分配性别的东西
const output = {
text: "He thought that his sweater would suit him"
}
我希望能够让对象在显示时随机显示 "He thought that his sweater would suit him" 或 "She thought that her sweater would suit her"。为了解决这个问题而不必从本质上创建两个不同的对象——一个用于男性,一个用于女性——我已经做到了这一点:
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 以及在调用对象时执行随机片段。有什么方法可以按照我列出的方式来做吗?
感谢您的帮助!
你需要 return 整个内部对象而不是某些道具,并且基于此你可以检索对象内部的道具:
function getRandGender() {
return Math.floor(Math.random() * 2) == 1 ? gender.female : gender.male
}
const randGender = getRandGender();
const output = {
text: `${randGender.pronoun} thought that ${randGender.possAdjective} sweater would suit ${randGender.object}`
}
这是因为您在分配 output.text
值时调用函数 randGender 三次不同,因此它会在每次调用时随机生成一个性别。
最好使用'randomizer'将变量定义为对象一次,然后在分配output.text
时调用该对象变量。
请参阅下面的代码段。
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"
}
};
const randomGender = Math.floor(Math.random() * 2) == 1 ?
gender.female :
gender.male;
console.log(`Random Gender:`, randomGender);
const output = {
text: `${randomGender.pronoun} thought that ${randomGender.possAdjective} sweater would suit ${randomGender.object}`
}
document.write(JSON.stringify(output));
我正在尝试编写从对象加载文本并以持续存在的方式随机分配性别的东西
const output = {
text: "He thought that his sweater would suit him"
}
我希望能够让对象在显示时随机显示 "He thought that his sweater would suit him" 或 "She thought that her sweater would suit her"。为了解决这个问题而不必从本质上创建两个不同的对象——一个用于男性,一个用于女性——我已经做到了这一点:
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 以及在调用对象时执行随机片段。有什么方法可以按照我列出的方式来做吗?
感谢您的帮助!
你需要 return 整个内部对象而不是某些道具,并且基于此你可以检索对象内部的道具:
function getRandGender() {
return Math.floor(Math.random() * 2) == 1 ? gender.female : gender.male
}
const randGender = getRandGender();
const output = {
text: `${randGender.pronoun} thought that ${randGender.possAdjective} sweater would suit ${randGender.object}`
}
这是因为您在分配 output.text
值时调用函数 randGender 三次不同,因此它会在每次调用时随机生成一个性别。
最好使用'randomizer'将变量定义为对象一次,然后在分配output.text
时调用该对象变量。
请参阅下面的代码段。
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"
}
};
const randomGender = Math.floor(Math.random() * 2) == 1 ?
gender.female :
gender.male;
console.log(`Random Gender:`, randomGender);
const output = {
text: `${randomGender.pronoun} thought that ${randomGender.possAdjective} sweater would suit ${randomGender.object}`
}
document.write(JSON.stringify(output));