简体   繁体   English

箭头功能运行完美,没有花括号。 添加了花括号{return}并且它会中断

[英]Arrow function runs perfect without curly braces. Added curly braces{ return } and it breaks

I rendered data from my objects to the DOM perfectly by using using arrow function without curly braces. 我使用不带花括号的箭头函数将数据从我的对象完美地渲染到DOM。

I tried adding curly braces after the same arrow function. 我尝试在相同的箭头功能后添加花括号。 The DOM will not render any data. DOM不会呈现任何数据。

CODE WORKS WITHOUT CURLY BRACES AFTER ARROW FUNCTION

function displayMatches () {
    const matchArray = findMatches(this.value, cities)
    console.log(matchArray)
    const html = matchArray.map(place => 
        `<li>
            <span class="name">${place.city}, ${place.state}</span>
            <span class="population">${place.population}</span>
        </li>`
    ).join('')
    suggestion.innerHTML = html

}


THE SAME CODE BREAKS WITH CURLY BRACES AFTER ARROW FUNCTION 

function displayMatches () {
    const matchArray = findMatches(this.value, cities)
    console.log(matchArray)
    const html = matchArray.map(place => {
        return
        `<li>
            <span class="name">${place.city}, ${place.state}</span>
            <span class="population">${place.population}</span>
        </li>`
    }).join('')
    suggestion.innerHTML = html

} 

You're falling victim to a "trap" in the semicolon insertion rules. 您正在成为分号插入规则中“陷阱”的受害者。 After a return , a semicolon is assumed if an expression doesn't start on the same line. return ,如果表达式不在同一行上开始,则假定使用分号。

If you change your function as follows, it should work: 如果您按如下方式更改功能,它应该工作:

function displayMatches () {
    const matchArray = findMatches(this.value, cities)
    console.log(matchArray)
    const html = matchArray.map(place => {
        return `<li>
            <span class="name">${place.city}, ${place.state}</span>
            <span class="population">${place.population}</span>
        </li>`
    }).join('')
    suggestion.innerHTML = html

} 

What happens is when you use arrow functions with and returning a value with below the return statement like this: 当你使用箭头函数并返回一个低于return语句的值时,会发生什么:

return
`My Statement` // unreachable code

You will get an error. 你会收到一个错误。 But if you do it like this: 但如果你这样做:

return `My Statement` //reachable code

It should fix your problem if you do it like this: 如果你这样做,它应该解决你的问题:

function displayMatches () {
    const matchArray = findMatches(this.value, cities)
    console.log(matchArray)
    const html = matchArray.map(place => {
        return `<li>
            <span class="name">${place.city}, ${place.state}</span>
            <span class="population">${place.population}</span>
        </li>`
    }).join('')
    suggestion.innerHTML = html

} 

the lack of curly braces in arrow function, means return the evaluation. 箭头函数中缺少花括号,表示返回评估。

so 所以

    matchArray.map(place => 
      place.state
    )

    // is equal to
    matchArray.map(place => {
      return place.state
    })

i suggest dig deeper into es6 arrow functions to understand better . 我建议深入研究es6 arrow功能,以便更好地理解。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM