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

jslint and memoizer function

Expand Messages
  • frankxberlin
    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 =
    Message 1 of 3 , May 18, 2011
    • 0 Attachment
      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?

      Defining var recur; in line 2?

      var memoizer = function (memo, formula) {
      var recur;
      recur = function (n) {
      var result = memo[n];
      if (typeof result !== 'number') {
      result = formula(recur, n);
      memo[n] = result;
      }
      return result;
      };
      return recur;
      };

      Or returning a named function expression?

      var memoizer = function (memo, formula) {
      return function recur(n) {
      var result = memo[n];
      if (typeof result !== 'number') {
      result = formula(recur, n);
      memo[n] = result;
      }
      return result;
      };
      };

      Thx, Robert from Berlin
    • Joshua Bell
      ... (snip) ... That would be using the language as designed: ECMA-262 [3rd and 5th editions]: The *Identifier *in a *FunctionExpression *can be referenced from
      Message 2 of 3 , 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]
      • Robert Sauer-Ernst
        Hi Josh, thanks. Now the difference between named function expression and function declaration seems clear (s. also http://kangax.github.com/nfe/ or
        Message 3 of 3 , May 18, 2011
        • 0 Attachment
          Hi Josh,

          thanks. Now the difference between "named function expression" and
          "function declaration" seems clear (s. also
          http://kangax.github.com/nfe/ or
          https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope).


          Just for my curiosity @Douglas: Why do you use an example that does not
          pass JSLint? Did you add that feature later to JSLint. Or was the
          memoizer-example just to show, how JS works, but not to show, how to
          write JSLint-compatible code?

          Thx again,

          Robert from Berlin

          --- Original Nachricht ---

          Von: Joshua Bell <josh@...>
          Am: Wed, 18 May 2011 11:36:36 -0700
          Betreff: Re: [jslint] jslint and memoizer function
          An: jslint_com@yahoogroups.com
          CC:
          >
          > On Wed, May 18, 2011 at 10:37 AM, frankxberlin
          > <facebook@... <mailto:facebook%40sauer-ernst.de>>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]
          >
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.