Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hasOwnProperty in the memoization pattern #6

Closed
mathiasbynens opened this issue Jan 13, 2012 · 3 comments
Closed

Use hasOwnProperty in the memoization pattern #6

mathiasbynens opened this issue Jan 13, 2012 · 3 comments

Comments

@mathiasbynens
Copy link

  var myFunc = function (param) {
    if (!myFunc.cache[param]) {
      var result = {};
      // ... expsensive operation ...
      myFunc.cache[param] = result;
    }
    return myFunc.cache[param];
  };

If param is e.g. "toString" this will cause problems.

({})['toString'] // function toString() { [native code] }

The solution is to use hasOwnProperty.

@chuanxshi
Copy link
Owner

@mathiasbynens thanx for pointing this out, will use the existing one as an antipattern and put up one with hasOwnProperty as the preferred way.

@RiZKiT
Copy link

RiZKiT commented Sep 12, 2014

Preferred method 3 seems to have the same problem, isn't it?

@ammojamo
Copy link

Yes it looks like it does, plus arguments.callee is deprecated (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants