Esnext: Comma is also allowed after the last parameter

Source: Internet
Author: User

Https://jeffmo.github.io/es-trailing-function-commas is currently a stage 3 proposal, Chakra and JSC have been implemented, It allows us to add a comma to the end of the last argument at the time of the function definition and when the last parameter is called by the function.

What are the advantages of the last parameter plus a comma?

Note: The advantage of the last parameter plus a comma is only reflected when the parameter is written in a branch.

Advantage 1: It is not easy to make mistakes when modifying code

If the current code looks like this:

function foo (    param1,    param2,    param3)

What you want to do now is to delete the implementation of the PARAM3 parameter, when you are likely to position the cursor to the PARAM3 line, continuous press command+x and Command+s (Sublime):

function foo (    param1,    param2,)

A test, syntaxerror! Suddenly, go back and then delete the comma.

If it is a new parameter, the problem is similar, you have to pay attention to the last parameter to fill the comma; Adjust the order of the parameters (Sublime with Ctrl + Command +↑↓) is similar. In addition to formal parameters, the arguments passed in when the function is called are also a reason.

If a comma has been added to each of the trailing parameters, then there is no such problem.

Advantage 2: More friendly to GIT/HG blame

I used git to generate a demo repository with a demo file Test.js:

I now want to know Param3 and the corresponding arg3 who is in which commit to add up, so I will execute git blame test.js:

Git blame told me that Xiao Li was in the 11E537FC this commit Riga. But when you look at the commit changes, you will find that, wrong, Xiao Li is only in the implementation of PARAM4 when forced to param3 a comma, complete git log-p test.js to see the truth, the real param3 is a small Zhang, but also can be found, Xiao Zhang in The implementation of PARAM3 was also forced to change the param2 line:

If there is a comma at the end of each line of arguments, then the developer does not need to modify the row that is unrelated to this commit, thus polluting the git/hg blame.

What are the advantages of the parameter branch writing?

Some students asked: "This proposal is useless to me, all my parameters are written in one line." Indeed, if you never write the parameters in a branch, you don't use that feature at all. However, the parameter Branch write is indeed useful:

Advantage 1: Can avoid the line too long

If there are too many arguments, or if an argument is a long string, then the parameter is written in a row, which causes the editor to appear in a horizontal scroll bar, which is neither beautiful nor readable.

Advantage 2: More friendly to GIT/HG blame

As shown earlier, if all of your parameters are written in one line, you cannot blame to the commit that each parameter was added to. Some of your classmates are skeptical? Did anyone really write that? It's true, there's a V8 warehouse on my computer, and we'll look it up with the following grep command:

grep -zopr--include='*.js'function \w+\ (\ n ([\s\w]+, (. +)? \ n) +[\ s\w]+\)'

Sure enough, I found one:

Git blame a bit:

The commit of each parameter is easy to get! This is important in open-source projects where people work together.

Advantage 3: Avoid branching conflicts

Obviously, two branches add new parameters on one line at the same time, which inevitably creates a conflict.

Advantage 4: Easy to add comments

If you need to add a comment for each parameter, do you have any choice but to write a branch? Also in V8 's warehouse grep to the real case, is to give each real to participate in the annotation:

These are the same for object literals and arrays of literals, right?

Yes, the advantages of the comma above and the advantages of the branch are the same for {} and [], {} is supported at the end of the ES5, and [] is supported at birth because it needs to support sparse arrays such as [1,,,].

What about JSON?

"When can the JSON specification change, not unified off very uncomfortable ah?" "Yes, I think so, but I don't know when I can change it."

How can I get into ES?

ES8 (2017) or ES9 (2018)? Don't care anymore, now is the first to implement the backward specification, just like the version number of Chrome, as it goes up. At present SpiderMonkey and V8 have not yet begun to realize, I have given V8 demand https://bugs.chromium.org/p/v8/issues/detail?id=5051

Several syntax limitations

Here are the SyntaxError:

function // no parameter, only a comma . function // two consecutive commas function // The remaining argument is followed by a comma, because the remaining argument must be the last parameter

Esnext: Comma is also allowed after the last parameter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.