Declaring Const With Curly Braces in JSX

前端 未结 2 578
醉梦人生
醉梦人生 2021-02-05 03:27

I\'m just getting started with React Native and getting used to JSX syntax. Is that what I\'m talking about? Or am I talking about TypeScript? Or... ES6? Anyway...

I\'ve

2条回答
  •  星月不相逢
    2021-02-05 03:52

    It is destructuring assignment.

    The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

    Example (ES6):

    var person = {firstname: 'john', lastname: 'doe'};
    
    const firstname = person.firstname;
    const lastname = person.lastname;
    
    // same as this
    const { firstname, lastname } = person;
    

    You can find more info at MDN

    EDIT: also for developers familiar with Python language it can be interesting to compare with Python unpacking syntax. Python2.7:

    >>> _tuple = (1, 2, 3)
    >>> a, b, c = _tuple
    >>> print(a, b, c)
    (1, 2, 3)
    

    With new feature of Python3, like PEP 3132 you can also do following:

    >>> _range = range(5)
    >>> a, *b, c = _range
    >>> print(a, b, c)
    0 [1, 2, 3] 4
    

    Examples are added, because knowing already similar approach from other languages you can grasp JS idea more quicker.

提交回复
热议问题