Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-1530

Function declarations should not be made within blocks

    XMLWordPrintable

    Details

    • Message:
      Do not use function declarations within blocks.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Legacy Key:
      FunctionDeclarationsWithinBlocks
    • Covered Languages:
      JavaScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min

      Description

      While most script engines support function declarations within blocks, from browser to browser, the implementations are inconsistent with each other.

      Noncompliant Code Example

      if (x) {
        function foo() {} //foo is hoisted in Chrome, Firefox and Safari, but not in Edge.
      }
      

      Compliant Solution

      if (x) {
        const foo = function() {}
      }
      

        Attachments

        1.
        JavaScript RSPEC-4302 Language-Specification Active Unassigned

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            freddy.mallet Freddy Mallet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: