我真的不明白为什么我们需要在下面的代码中应用AND
的bitwise BindingFlags.Types
:
switch (binding.flags & BindingFlags.Types) {
case BindingFlags.TypeElementAttribute:
setElementAttribute(view, binding, renderNode, binding.ns, name, value);
break;
case BindingFlags.TypeElementClass:
setElementClass(view, renderNode, name, value);
break;
case BindingFlags.TypeElementStyle:
setElementStyle(view, binding, renderNode, name, value);
break;
case BindingFlags.TypeProperty:
const bindView = (def.flags & NodeFlags.ComponentView &&
binding.flags & BindingFlags.SyntheticHostProperty) ?
elData.componentView :
view;
setElementProperty(bindView, binding, renderNode, name, value);
break;
}
这是BindingFlags
的定义:
export const enum BindingFlags {
TypeElementAttribute = 1 << 0,
TypeElementClass = 1 << 1,
TypeElementStyle = 1 << 2,
TypeProperty = 1 << 3,
SyntheticProperty = 1 << 4,
SyntheticHostProperty = 1 << 5,
CatSyntheticProperty = SyntheticProperty | SyntheticHostProperty,
// mutually exclusive values...
Types = TypeElementAttribute | TypeElementClass | TypeElementStyle | TypeProperty
}
有人可以澄清一下吗?
我真的不明白为什么我们需要在下面的代码中应用BindingFlags.Types的逻辑AND:
它是一个按位AND(&
),而不是一个逻辑的(&&
)。
你需要它来过滤掉其他位(例如qazxsw poi)。 qazxsw poi只包括SyntheticProperty
位。
例如,如果BindingFlags.Type
同时具有TypeXYZ
(0b00010000)和binding.flags
(0b00000010),则其值为0b00010010。由于SyntheticProperty
只需匹配类型的标志,因此它使用TypeElementClass
(0b00001111)和switch
来屏蔽非类型值。 0b00010010和0b00001111是0b0001000,因此它匹配BindingFlags.Types
案例。