Loading ...
Sorry, an error occurred while loading the content.

2218Re: [jslint] jslint and memoizer function

Expand Messages
  • Joshua Bell
    May 18, 2011
    • 0 Attachment
      On Wed, May 18, 2011 at 10:37 AM, frankxberlin <facebook@...>wrote:

      > Hello,
      >
      > in the "Good parts" and the latest talk on etsy if find as an example:
      >
      > var memoizer = function (memo, formula) {
      > var recur = function (n) {
      > var result = memo[n];
      > if (typeof result !== 'number') {
      > result = formula(recur, n);
      > memo[n] = result;
      > }
      > return result;
      > };
      > return recur;
      > };
      >
      > This throws an error at JSLint: "Problem at line 5 character 30: 'recur'
      > has not been fully defined yet."
      >
      > How do you fix it?
      >

      (snip)


      > Or returning a named function expression?
      >

      That would be using the language as designed:

      ECMA-262 [3rd and 5th editions]: The *Identifier *in a *FunctionExpression *can
      be referenced from inside the *FunctionExpression's FunctionBody *to allow
      the function to call itself recursively. However, unlike in a *
      FunctionDeclaration*, the *Identifier *in a *FunctionExpression *cannot be
      referenced from and does not affect the scope enclosing the *
      FunctionExpression*.

      ... so that gets my vote.

      -- Josh


      [Non-text portions of this message have been removed]
    • Show all 3 messages in this topic