问题 displayCartTotal,使用formatAsMoney函数将appState.billFormated设置为格式化的总帐单。已经分配的appState.bill和appState.country现在应该派上用场了!
将数据清单SPAN的文本内容设置为在appState.billFormated中设置的格式化票据
最后,调用uiCanInteract来包装displayCartTotal。
!!
我的代码
> Blockquote
const formatAsMoney = (amount, buyerCountry) => {
const country = countries.find(c => c.country === buyerCountry);
const {code, currency} = country;
if (code && currency) return amount.toLocaleString(code, {style: "currency", currency});
return amount.toLocaleString("en-us", {style: "currency", currency: "USD"});
};
const detectCardType = ({target}) => {
}
const validateCardExpiryDate = ({target}) => {
};
const validateCardHolderName = ({target}) => {
};
const uiCanInteract = () => {
};
const displayCartTotal = ({results}) => {
const {results} = results;
const [data] = results;
const {itemsInCart, buyerCountry} = data;
appState.items = itemsInCart;
appState.country = buyerCountry;
appState.bill = itemsInCart.reduce((result, item,) => result = item.price * item.qty, 0);
appState.billFormated = formatAsMoney (appState.bill, appState.country);
databill.textContent = appstate.billFormated;
uiCanInteract();
};
想要找到如何有效解决以上发布的问题的指针!
答案 0 :(得分:0)
这可能就是你的意思
const displayCartTotal = (resultingStuff) => {
const {results} = resultingStuff;
const [data] = results;
const {itemsInCart, buyerCountry} = data;
appState.items = itemsInCart;
appState.country = buyerCountry;
appState.bill = itemsInCart.reduce((result, item,) => result = item.price * item.qty, 0);
appState.billFormated = formatAsMoney (appState.bill, appState.country);
databill.textContent = appstate.billFormated;
uiCanInteract();
};
const blah = ({result}) => {
const {result} = result
document.write(result)
}
blah({result: { result: 'apple' } })
/*
SyntaxError: Unexpected token '}'. Cannot declare a lexical variable twice: 'result'.
*/
SyntaxError:意外令牌'}'。无法两次声明词法变量:“结果”。 您已经在相同的词法范围内定义了2倍的结果,并且JavaScript(也没有人读过此书)对所引用的结果有任何了解。
请注意,通常来说:使用诸如results
和data
这样模糊的变量名是不明智的。哪种类型的results
,还可以帮助您避免意外地将参数命名为与您尝试分配的变量之一相同的变量:
请注意:
const displayCartTotal = ({results}) =>
等效于:
const displayCartTotal = (stuff) => {
const results = stuff.results
表示您已经分配了结果...您还确定该对象确实两次包含results
吗?
因为这是您的代码所做的:
const displayCartTotal = (stuff) => {
const results = stuff.results
const results = results.results // UNHAPPY: cannot declare lexical variable twice
如果api确实答复:
{
"results": {
"results": {
"data": []
}
}
那么最好的选择就是做
const displayCartTotal = ({results}) => {
const { results: innerResults } = results
const { data } = innerResults
或者您可以在一行中完成以上操作:
const displayCartTotal = ({results: { results: { data } }) => {
// Now only data will be defined but it looks like that's all you need.