我有反对意见
dataSourceConfig.transport.read.url
remoteDataSourceConfig: {
transport: {
read: {
url: null,
},
},
},
如何检查 dataSourceConfig.transport.read.url
是否不安全?
最佳答案
如果我正确理解你的问题,你必须检查所有中间对象是否存在:
if (dataSourceConfig &&
dataSourceConfig.transport &&
dataSourceConfig.transport.read &&
dataSourceConfig.transport.read.url != null
) {
// is not null or undefined
}
否则你可以这样做:
function isKeySet(obj, key) {
var keys = key.split('.');
return keys.reduce(function(o,k){ return o[k]; }, obj) != null;
}
isKeySet(dataSourceConfig, 'transport.read.url'); // false
关于javascript - 如何检查 undefined object ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17101585/