Array destructuring in JavaScript

前端 未结 5 622
情歌与酒
情歌与酒 2020-11-30 12:21

I have this code in my vue-js app:

methods: {
    onSubmit() {
      ApiService.post(\'auth/sign_in\', {
        email: this.email,
        password: this.pa         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 12:41

    Always look things up on MDN if you want to find out about javascript things. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring

    Here's a simple example of destructuring:

    const [a, b] = ['a', 'b'];
    

    Its a shorthand available since es6 that allows doing variable assignment in a more shorthand way.

    The original way would be like:

    const arr = ['a', 'b'];
    const a = arr[0];
    const b = arr[1];
    

    And the es6 way would be like:

    const arr = ['a', 'b'];
    const [a, b] = arr;
    

    Now in regards to the eslint error, I actually disagree with that one. Your code by itself should be fine. So you should file an issue on the Eslint github repo to ask about why that line is triggering the "prefer-destructuring" warning.

提交回复
热议问题