• 函数表达式:

    1. (() => {
    2. console.log('Welcome to the Internet. Please follow me.');
    3. })();
  • 注意: ECMA-262 把 block 定义为一组语句。函数声明不是语句。。

  • 7.5 永远不要把参数命名为 arguments。这将取代原来函数作用域内的 arguments 对象。

    1. // bad
    2. function nope(name, options, arguments) {
    3. // ...stuff...
    4. }
    5. function yup(name, options, args) {
    6. // ...stuff...
    7. }

  • 7.7 直接给函数的参数指定默认值,不要使用一个变化的函数参数。

    1. // really bad
    2. function handleThings(opts) {
    3. // 不!我们不应该改变函数参数。
    4. // 更加糟糕: 如果参数 opts 是 false 的话,它就会被设定为一个对象。
    5. // 但这样的写法会造成一些 Bugs。
    6. //(译注:例如当 opts 被赋值为空字符串,opts 仍然会被下一行代码设定为一个空对象。)
    7. // ...
    8. // still bad
    9. function handleThings(opts) {
    10. if (opts === void 0) {
    11. opts = {};
    12. }
    13. // ...
    14. }
    15. // good
    16. function handleThings(opts = {}) {
    17. }