这个三元表达式在做什么?
What is this ternary expression doing?
我有一个正在测试的函数,它接受两个参数定义和元素,并且其中有一个三元语句,就像
otherThingName: (_.has(Definition.thing, 'thingName') ? Element[Definition.thing.thingName] : null)
现在 Definition.thing.thingName
将存在,但元素没有 属性 命名定义。
元素上是否设置了 属性 而设置为相同的设置 otherThingName
?
三元表达式是一种简写 if/else,因此在第一个实例中,它测试语句 (_.has(Definition.thing, 'thingName')
.
我不使用下划线,但看起来这个测试正在检查 Definition.thing
是否具有 thingName
的 属性。
如果返回 true,它将 otherThingName
设置为 Element[Definition.thing.thingName]
。
否则会设置为null
。
Element[Definition.thing.thingName]
正在查看一个名为 Element
的对象,并使用与 Definition.thing.thingName
.
的值匹配的键拉回 属性
例如,如果
Definition.thing.thingName == "name"
,
Element[Definition.thing.thingName] == Element["name"] == Element.name
.
希望这对您有所帮助。
展开文本会变得更加清晰:
var str;
if (_.has(Definition.thing, 'thingName')) {
str = Element[Definition.thing.thingName]
} else {
str = null;
}
...
otherThingName: str
看起来它正在将某个对象 'otherThingName' 的成员定义为 Element 为字段 Definition.thing.thingName 设置的任何内容(如果存在),否则为 null。
我有一个正在测试的函数,它接受两个参数定义和元素,并且其中有一个三元语句,就像
otherThingName: (_.has(Definition.thing, 'thingName') ? Element[Definition.thing.thingName] : null)
现在 Definition.thing.thingName
将存在,但元素没有 属性 命名定义。
元素上是否设置了 属性 而设置为相同的设置 otherThingName
?
三元表达式是一种简写 if/else,因此在第一个实例中,它测试语句 (_.has(Definition.thing, 'thingName')
.
我不使用下划线,但看起来这个测试正在检查 Definition.thing
是否具有 thingName
的 属性。
如果返回 true,它将 otherThingName
设置为 Element[Definition.thing.thingName]
。
否则会设置为null
。
Element[Definition.thing.thingName]
正在查看一个名为 Element
的对象,并使用与 Definition.thing.thingName
.
例如,如果
Definition.thing.thingName == "name"
,
Element[Definition.thing.thingName] == Element["name"] == Element.name
.
希望这对您有所帮助。
展开文本会变得更加清晰:
var str;
if (_.has(Definition.thing, 'thingName')) {
str = Element[Definition.thing.thingName]
} else {
str = null;
}
...
otherThingName: str
看起来它正在将某个对象 'otherThingName' 的成员定义为 Element 为字段 Definition.thing.thingName 设置的任何内容(如果存在),否则为 null。