为什么javascript让您在函数参数的末尾添加逗号?

时间:2019-02-21 11:11:08

标签: javascript ecmascript-6 ecmascript-2017 ecmascript-7

我在javascript函数声明中遇到了似乎已关闭的更改。您可以创建如下函数:

let a = function (b,) {
    console.log(b);
}

我发现由于git之间的差异,允许在函数参数中使用逗号逗号

let a = function (
    b,
) {
    console.log(b);
}

let a = function (
    b,
    c,
) {
    console.log(b);
}

git diffs确实是这样做的原因,因为我相信只有ECMAScript-2017才能实现。

2 个答案:

答案 0 :(得分:1)

{ "name": "sigma", "version": "1.0.0", "private": true, "dependencies": { "approve": "0.0.12", "approvejs": "^3.1.1", "axios": "^0.18.0", "babel-polyfill": "^6.26.0", "bootstrap": "^4.1.1", "chartist": "^0.11.0", "d3": "^5.5.0", "font-awesome": "^4.7.0", "froala-editor": "^2.9.1", "gulp": "^3.9.1", "harmony-reflect": "^1.6.0", "i18n": "^0.8.3", "ionicons": "^4.1.2", "lodash": "^4.17.10", "mgr-pdf-viewer-react": "^1.0.1", "minimist": "^1.2.0", "moment": "^2.22.2", "mustache": "^2.3.0", "prop-types": "^15.5.10", "rc-slider": "^8.3.1", "react": "^16.4.1", "react-chartist": "^0.13.0", "react-datamaps": "^0.4.1", "react-datetime": "^2.9.0", "react-dom": "^16.4.0", "react-easy-chart": "^1.0.0", "react-faux-dom": "^4.2.0", "react-froala-wysiwyg": "^2.9.1-1", "react-google-maps": "^9.4.5", "react-intl-tel-input": "^5.0.5", "react-loadable": "^5.4.0", "react-places-autocomplete": "^7.2.0", "react-rating": "^1.3.0", "react-redux": "^5.0.6", "react-render-html": "^0.6.0", "react-router-dom": "^4.0.0-beta.8", "react-router-redux": "^5.0.0-alpha.6", "react-scripts": "^1.0.11", "react-select": "^1.0.0-rc.5", "react-spinners": "^0.3.2", "react-transition-group": "^2.3.1", "reactable": "^0.14.1", "reactstrap": "^6.0.1", "reapop": "^1.1.2", "recharts": "^1.0.0-alpha.3", "recompose": "^0.27.1", "redux": "^4.0.0", "redux-thunk": "^2.2.0", "serve-favicon": "^2.5.0", "socket.io": "^2.1.1", "socket.io-client": "^2.1.1", "source-map": "^0.7.3", "source-map-explorer": "^1.4.0" }, "devDependencies": { "node-sass-chokidar": "^1.3.0", "npm-run-all": "^4.1.3" }, "babel": { "presets": [ [ "env", { "modules": false } ], "react" ] }, "scripts": { "analyze": "source-map-explorer build/static/js/main.*", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject", "build-css": "node-sass-chokidar --include-path ./src/scss --include-path ./node_modules src/scss -o src/css", "watch-css": "npm run build-css && node-sass-chokidar --include-path ./src/scss --include-path ./node_modules src/scss -o src/css --watch --recursive", "start-js": "react-scripts start", "start": "npm-run-all -p watch-css start-js", "build": "npm run build-css && react-scripts build" } } 来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas
规范允许。
我认为此功能的唯一原因不是帮助控制系统(例如ECMAScript 2017 allows trailing commas in function parameter lists.)进行跟踪,甚至id也是一个不错的选择...
另一个原因是,重新排列项目更简单,因为如果最后一个项目改变了位置,则不必添加和删除逗号。据我说,这是主要原因。在介绍它之前,我总是问自己为什么JS对我们可怜的开发人员如此严格...:-)

答案 1 :(得分:0)

  

git diffs确实是它起作用的原因,我相信只有ECMAScript-2017。

基本上,答案是肯定的。引用原始的proposal(粗体字是我的)

  

在某些代码库/样式指南中,会出现以下情况:   函数调用和定义在   的风格:

 1: function clownPuppiesEverywhere(
 2:   param1,
 3:   param2
 4: ) { /* ... */ }
 5: 
 6: clownPuppiesEverywhere(
 7:   'foo',
 8:   'bar'
 9: );
  

在这种情况下,当其他一些代码贡献者出现并添加时   这些参数列表之一的另一个参数,它们必须使两个   线路更新

 1: function clownPuppiesEverywhere(
 2:   param1,
 3:   param2, // updated to add a comma
 4:   param3  // updated to add new parameter
 5: ) { /* ... */ }
 6: 
 7: clownPuppiesEverywhere(
 8:   'foo',
 9:   'bar', // updated to add a comma
10:   'baz'  // updated to add new parameter
11: );
  

在对版本管理的代码进行此更改的过程中   控制系统(git,Subversion,Mercurial等),责任/注释   第3行和第9行的代码历史记录信息已更新为指向   添加逗号的人(而不是最初添加逗号的人   添加了参数)。

     

为帮助缓解此问题,其他一些语言(Python,D,Hack,   ...可能是其他...)添加了语法支持以允许结尾   这些参数列表中的逗号。 这使代码贡献者能够   总是在其中一个结尾的末尾加上逗号   每行参数列表,无需担心代码   再次归因问题