Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

函数参数的尾部逗号 #2

Open
chenxiaochun opened this issue Aug 8, 2017 · 0 comments
Open

函数参数的尾部逗号 #2

chenxiaochun opened this issue Aug 8, 2017 · 0 comments

Comments

@chenxiaochun
Copy link
Owner

chenxiaochun commented Aug 8, 2017

现在声明函数的时候,可以在参数尾部添加逗号了。

function foo(
    param1,
    param2,
) {}

同理,调用函数时也可以这样用:

foo(
    'abc',
    'def',
);

在一个对象中使用会被忽略:

let obj = {
    first: 'Jane',
    last: 'Doe',
};

for(o in obj){
    console.log(o)
}
'first'
'last'

同样,在数组中使用也会被忽略:

let arr = [
    'red',
    'green',
    'blue',
];
console.log(arr.length);
3

使用函数尾部参数语法有两个好处:

  1. 维护代码时更简单,当修改的代码是最后一个参数位置时,不需要再增删最后那个逗号。
  2. 增加一个参数,在版本控制系统中查看代码时,会发现仅仅是添加参数的位置有改动记录。
foo(
    'abc',
);

改成:

foo(
    'abc',
    'def',
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant