简体   繁体   English

扩展javascript(ES6)箭头符号(简写)为完整语法?

[英]expanding javascript(ES6) arrow notation (shorthand) to full syntax?

I am relatively new to javascript and ive been learning React. 我对javascript比较陌生,我一直在学习React。 I followed a few tutorials for fetching data from an API in React using fetch(). 我遵循了一些教程,使用fetch()从React中的API提取数据。 The tutorial worked fine of course, and when recreating it with a different API it took me hours to figure out how to reproduce it even copying the shorthand code, so I've been trying to expand this code, so I can understand exactly what is going on. 该教程当然运行良好,并且当使用其他API重新创建它时,我花了数小时才弄清楚即使复制速记代码也要重现它,因此我一直在尝试扩展此代码,因此我可以确切地了解什么是继续。

fetch(request)
    .then(response => response.json())
    .then(data => { 
        let drinkNames = data.data.map(
            (dataSelect) => {
            return (
                <div key={dataSelect.data}>
                {dataSelect.name}
                </div >
            )
        })
        this.setState({drinks:drinkNames});
    })

That is theshorthand code and it contains what would seem like embeded functions that return other functions, but when trying to expand it everything I try gives me an error, and I would like to understand what is going on. 那是简写代码,它包含看起来像嵌入函数的函数,该函数返回其他函数,但是当尝试对其进行扩展时,我尝试的一切都会给我一个错误,并且我想了解发生了什么。 Can anybody help me expand that code? 有人可以帮我扩展该代码吗?

I read from the documentation that in its basic use, the arrow function returns whatever is in brackets. 我从文档中了解到,箭头函数在其基本用法中会返回括号中的内容。 Following that logic, i tried 按照这种逻辑,我尝试了

then(function(data){

        function longhand(){
            let drinkNames = data.data.map(
            (dataSelect) => {
                return (
                    <div key={dataSelect.data}>
                    {dataSelect.name}
                    </div >
                )
            })
        }

        return longhand();

This obviously doesnt work, and it seems there is another shorthand arrow function inside my new longhand() function, so I'm overall a bit confused. 这显然行不通,而且似乎在我的新longhand()函数中还有另一个速记箭头函数,所以总体上我有点困惑。 help? 救命?

The arrow functions expand in the following manner: 箭头功能以以下方式展开:

then(data => data.id)

is equal to: 等于:

then(function(data) {
    return data.id
});

So, your original code in ES5 syntax would look like this: 因此,使用ES5语法的原始代码如下所示:

fetch(request)
    .then(function(response) {
        return response.json()
    })
    .then(function(data) { 
        let drinkNames = data.data.map(function(dataSelect) {
            return (
                <div key={dataSelect.data}>
                    {dataSelect.name}
                </div >
            )
        })
        this.setState({drinks:drinkNames});
    })

UPDATE: 更新:

As it was correctly mentioned in the comment the context of this will change, as arrow functions unlike regular functions don't create their own context. 由于这是在评论中提到正确的情况下this将改变,因为不像普通功能箭头的功能不会产生自己的上下文。 Without seeing your entire component I couldn't tell you with a 100% certainty what this will change to, however I suspect that it will be Window or undefined. 没有看到你的整个组件我不能告诉你有100%的把握什么this将改变,但我怀疑,这将是窗口或不确定。 You can always use Function.prototype.bind to explicitly tell the function what this should refer to. 您可以随时使用Function.prototype.bind明确地告诉哪些功能this应该参考。

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

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