Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:33:42+00:00 2026-06-18T00:33:42+00:00

I am reading Secrets of the JavaScript Ninja by John Resig and Bear Bibeault

  • 0

I am reading Secrets of the JavaScript Ninja by John Resig and Bear Bibeault, currently up to Chapter 5 on closures. I think have the general gist of closures and how JavaScript operates with function scoping rather than the block scoping of C++/Java of which I am familiar. One thing I am having trouble understanding fully though is sometimes in the code there seem to be more function declarations than I can figure out purposes for. For instance, in Section 5.5.1 He covers self-memoizing functions, which is basically a function that remembers computed values and stores them in a cache for subsequent calls with the same input. He gives the following code:

Function.prototype.memoized = function (key) {                    // #1
    this._values = this._values || {};
    xyz = this._values;
    return this._values[key] !== undefined ?
        this._values[key] :
        this._values[key] = this.apply(this, arguments);
};

Function.prototype.memoize = function () {                        // #2
    var fn = this;
    return function() {                                           // #2.1
        return fn.memoized.apply(fn, arguments);
    };
};

var isPrime = (function (num) {
    var prime = num !== 1;

    for (var i = 2; i < num; i++) {
        if (num % i === 0) {
            prime = false;
            break;
        }
    }

    return prime;
}).memoize();

I understand most of this code. However what I am having trouble understanding is why at the second memoization function couldn’t simply be:

Function.prototype.memoize = function () {                      
    return this.memoized.apply(fn, arguments);
};

Can anyone give me a good explanation of what is happening in his code above and how it is different than what I have written here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:33:44+00:00Added an answer on June 18, 2026 at 12:33 am

    There is quite a big difference:

    return function() {
        return fn.memoized.apply(fn, arguments);
    };
    

    returns a function that you can call later. memoized is only called when the function is called.

    return this.memoized.apply(fn, arguments);
    

    calls the function memoized and returns its return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading the book Secrets of the JavaScript Ninja by John Resig and
I was reading John Resig's Secrets of JavaScript Ninja and saw this code: function
I am reading this book Secrets of the Javascript Ninja where most of the
Reading what is currently the top answer to a recent question on how/whether to
Reading up on design patterns, and have found that there are 23(?) some-odd patterns
I have been reading documents/urls and really not understand about app.use and its usage.
I'm reading a small tutorial on javascript private members and public members ( http://www.crockford.com/javascript/private.html
I'm currently reading the scala version of play's yabe tutorial. yabe stands for yet
I have a jabber client that is reading from its stdin and posting PubSub
I have a Javascript application that uses Java as a backend with Hibernate, Spring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.