typescript - 访问块外的变量
问题描述
我是 TypeScript 的初学者(很明显,我来自 java 世界)。我目前正在尝试做一个这样的映射器:
public getApercuTypePrestationFromTypePrestationEX044(typePrestationEX044: TypePrestationEX044): ApercuTypePrestation {
let apercuTypePrestation: ApercuTypePrestation;
if (null != typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation == typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation == typePrestationEX044.libelle;
}
console.log("A = " + typePrestationEX044.code);
console.log("B = " + apercuTypePrestation.libelleTypePrestation);
return apercuTypePrestation;
}
但这显然不起作用:在控制台中,我有: A = A8C B = undefined
我该如何解决?
解决方案
您正在使用==
而不是=
. 我已经更改==
为=
,它现在应该可以工作了。
public getApercuTypePrestationFromTypePrestationEX044(typePrestationEX044: TypePrestationEX044): ApercuTypePrestation {
let apercuTypePrestation: ApercuTypePrestation;
if (null != typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
}
console.log("A = " + typePrestationEX044.code);
console.log("B = " + apercuTypePrestation.libelleTypePrestation);
return apercuTypePrestation;
}
在打字稿中,
==
或===
用于比较而不是分配,以便分配您必须使用的值=
更新
我还注意到您typePrestationEX044
以错误的方式检查 null 。
改变这个:
if (null != typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
}
至此
if (typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
}
此if
条件将自动检查undefined
,null
或boolean
推荐阅读
- javascript - 使用循环的节点 js 和 ejs
- r - 用循环填充数据框
- xml - 如何在开发者控制台匿名窗口中测试这个编写 XML Salesforce 文档?
- html - 将单选按钮的标签移动到单选按钮上方
- c# - 如何在 UWP 应用中通过 log4net 配置日志记录
- objective-c - 带有 WebView 的全屏(信息亭)NSWindowController 需要先单击才能单击任何链接
- perl - 在安装时使用 ExtUtils::MakeMaker/Dist::Zilla (dzil) 安装附加文件
- f# - F# 文件类型图标丢失
- r - 匹配r中同一列中的两个名称
- c++ - 检查没有转换的scanf格式