鉴于这种:
var metadata = {
title: 'Scratchpad',
translations: [
{
title: 'JavaScript-Umgebung'
}
]
};
const output = ({
title: englishTitle,
translations: [{
title: localeTitle
}]
}) => (
console.log(englishTitle), // "Scratchpad"
console.log(localeTitle) // "JavaScript-Umgebung"
)
output(metadata);
现在translations
是未定义的:
console.log(JSON.stringify(translations)) // ❌ "undefined"
问题:有没有办法translations
在参数解构表达式中创建变量(例如,在函数参数括号中)
您可以分解翻译及其内部成员:
var metadata = {
title: 'Scratchpad',
translations: [
{
title: 'JavaScript-Umgebung'
}
]
};
const output = ({
title: englishTitle,
translations, // get translations
translations: [{
title: localeTitle
}]
}) => (
console.log(englishTitle), // "Scratchpad"
console.log(localeTitle), // "JavaScript-Umgebung"
console.log(translations) // [{ title: 'JavaScript-Umgebung' }]
)
output(metadata);
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras