Javascript 当一个工作正常,另一个不正常时,需要在下面或陈述中明确一些';T

Javascript 当一个工作正常,另一个不正常时,需要在下面或陈述中明确一些';T,javascript,if-statement,Javascript,If Statement,将代词视为对象中的作用域变量(例如,let person ={})方法(例如this.bio=function(){})和this.gender作为对象属性 这段代码工作得很好:根据包含的性别返回适当的代词“he/she/they” if(this.gender === "m" || this.gender === "male" || this.gender === "M" || this.gender === "Male&q

将代词视为对象中的作用域变量(例如,let person ={})方法(例如this.bio=function(){})和this.gender作为对象属性

这段代码工作得很好:根据包含的性别返回适当的代词“he/she/they”

if(this.gender === "m" || this.gender === "male" || this.gender === "M" || this.gender === "Male") {
pronoun = "he";
} else if(this.gender === "f" || this.gender === "female" || this.gender === "F" || this.gender === "Female") {
pronoun = "she"
} else {
pronoun = "they"
}
当这段代码运行时,只返回“he”


谁能帮我理解为什么??谢谢。

如果您打开控制台并编写
if('''{console.log('hi')}
它不会进行控制台操作,因为空字符串是一个错误值,所以它不会出现在if语句中。但是,
if('a')
中的任何字符串都可以作为控制台,因为它是真的。所以在你的代码中,如果this.gender不等于“m”,你说的是或“male”,在if语句中总是返回true。这就是为什么你会得到“他”:)
if
将首先检查
this.gender==“m”
。然后它将选中“男性”。这是一个价值观。所以,它总是会进入第一个区块,因为
“m”| | |“male”|“m”| |“male”=“m”
,你的可能在
[“m”、“male”、“m”、“male”]之后。包括(this.gender)
@HalilCakar好的,有意义。我认为JS会考虑这个(表达式)和它工作的地方一样(第一段代码(if)表达式)。谢谢你帮助mw理解这一区别,我很感激:)@adiga谢谢,现在有意义了,感谢Halil的解释!!我感谢你的时间和努力:)
if(this.gender === "m" || "male" || "M" || "Male") {
pronoun = "he";
} else if(this.gender === "f" || "female" || "F" || "Female") {
pronoun = "she"
} else {
pronoun = "they"
}