[英]What is wrong with the JavaScript Mapping Below?
const myAssetMap = {
General: {
name: 'Common',
bkgHex: '#eee',
fontHex: '#aaa',
},
...
const getAsset = (name, type) => {
return myAssetMap.name.type;
}
console.log( getAsset('General', 'name') );
This is erring with: Uncaught TypeError: Cannot read property 'type' of undefined
这是错误的:
Uncaught TypeError: Cannot read property 'type' of undefined
You are trying to access the keys name
and type
, not the keys stored in the variables. 您正在尝试访问键的
name
和type
,而不是访问存储在变量中的键。 Use bracket notation instead: 请改用方括号表示法:
return myAssetMap[name][type];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.