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 8513925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:43:12+00:00 2026-06-11T04:43:12+00:00

I am using John Resig’s inheritance implementation for my current project, and I was

  • 0

I am using John Resig’s inheritance implementation for my current project, and I was wondering if there is a way for subclass to also inherit(access) parent’s closure variable…

for example, I write the following class

var Foo = (function() {  
  var p = "im p";

  var Foo = Class.extend({  
    getp : function() {
       return p;
    }
  });

  return Foo;
})();

now Foo class has access to the variable p in the closure. so are Foo’s subclass…

var Bar = Foo.extend({});  
var bar = new Bar;
bar.getp(); // "im p"

this is no surprise since bar.getp simply call Foo.getp, which has access to p. however if i overwrite bar.getp

var Bar = Foo.extend({
  getp : function() {
    return p;
  }
});

now when I do bar.getp(), it will throw p is undefined since it is not accessible to bar

I have several methods in mind to make p accessible to bar, but I think they are little awkward, what you think is the cleanest way to accomplish this.

  • 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-11T04:43:13+00:00Added an answer on June 11, 2026 at 4:43 am

    Closure scope consists of anything that’s in scope when the function is defined. In this case, p is in scope when the first getp is defined, but not when the second getp is defined. So you can’t reference p. In fact, what you might have discovered is that the p referenced by the first getp method is actually universal to all Foo objects (and their sub-classes).

    JavaScript doesn’t really lend itself to “private variables” very well. Generally, developers end up using either closure scoped variables when inheritance is not needed or some sort of convention when inheritance is needed, such as adding an underscore, like this:

    var Foo = Class.extend({  
        _p : "i'm p",
        getp : function() {
            return this._p;
        }
     });
    

    Then you can subclass without difficulty:

    var Bar = Foo.extend({  
        getp : function() {
            return this._p;
        }
    });
    

    Now, I should also note that your example is pretty contrived and is contrived in a way as to be a bit pointless. I mean, why override getp() with something that also just returns p? If your intention was to actually create a private, static property p, then what you have should work fine. If you want access to p in subclasses, you should just use the getp method that you have access to, like this:

    var Bar = Foo.extend({  
        getpete : function() {
            return Foo.getp() + "ete"; 
            //You can also use this.getp() if you want, there's no difference.
        }
    });
    

    Hope that helps!

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

Sidebar

Related Questions

I am using John resig's implementation class mentioned here: http://ejohn.org/blog/simple-javascript-inheritance/ Now I have a
I'm using John Resig's excellent javascript class for simple javascript inheritance and I'm having
I am using John Resig's Simple JavaScript Inheritance and have run into an issue
I have a custom Javascript class (created using John Resig's Simple Javascript Inheritance ).
I'm using John Resig's recipe for JavaScript 'classes' and inheritance. I've stripped my code
I am using John Resig's Simple JavaScript Inheritance to create a class that can
I decided to try out JavaScript genius John Resig's simple JavaScript inheritance as detailed
I am using John Resig's JavaScript class definition style . Below is an example
I've been using John Resig's getStyle function from Pro JavaScript Techniques to get the
I'm using the Underscore template (which is based on John Resig's Microtemplate) and whenever

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.