上问题:
代码
requestMessage(type: string,data: any,header?:BaseMessageHeader): Promise<BaseMessageResponse<any>> {
const request: BaseMessageRequest= {
type: type,
data: data,
header:{
targetClientId: header?.targetClientId || this.option.targetClientId || '',
originClientId: header?.originClientId || this.clientId,
senderClientId: this.clientId,
isResponse: false,
keepConnection: header?.keepConnection
}
}
两跟竖线 “”||“”
我翻了一遍帮助文档,各类东西,既然没有相关的说明
怎么办,我查了许久,没有相关的资料。
没办法,我只能将这种写法抽出来,弄个单元测试,
代码如下
type ExtensionOption = {
//isBackground: boolean
haha:string;
}
function a(id:string,options?:ExtensionOption) {
this.option = options || {
asServer: true,
timeout: 8000,
isBackground: true,
};
console.log(id+this.option["haha"])//这里尝试下:传入asServer
}
let c:ExtensionOption={haha:"我来啦"};
a("haha",c)
这就明白了啊。
这种写法的意思是:如果参数存在,则选择参数中的值。
如果参数为undefined,则为||后面设置的值。
感叹到
为啥啊,我百度的上面,csdn上面都搜索不到,这是为啥呀,如果每次遇到类似的问题,然道都需要这样子测试才能出结果么。
加油吧 “勤年”!随叫我要搞定一个开源项目呀。