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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:22:32+00:00 2026-06-11T23:22:32+00:00

I thought that functions created using .prototype were supposed to be able to access

  • 0

I thought that functions created using .prototype were supposed to be able to access local variables. Here is what I’m talking about:

function obj() {
    var a = "String";

    this.innerPrint = function() {
        document.write("<p>" + a + "</p>");
    };
}

obj.prototype.outerPrint = function() {
    document.write("<p>" + a + "</p>");
};

var inst = new obj();
inst.innerPrint();
inst.outerPrint();

What I thought would happen is that both of these functions would do the same thing. But what actually happened is outerPrint doesn’t have access to the variable a. Could someone explain to me how you get locals in a prototype function.

Here is a fiddle running this code:
http://jsfiddle.net/Wryte/mxXzg/

By the way, I want to use prototype functions because then each instantiation of the object doesn’t have a copy of the function but they each point to the same one.

  • 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-11T23:22:34+00:00Added an answer on June 11, 2026 at 11:22 pm

    Of course not, you cannot access one function’s locals from another function defined outside the first. It does not matter if the second is defined in the prototype property of the first. Each invocation of your obj function defines a new local a variable, so you cannot avoid having separate copies of the function that needs to access it.

    I assume you actually require that variable to be a local and not an instance property, so one possible approach would be to have only a thin local wrapper around your function:

    function obj() {
        var a = "String";
    
        this.wrapper = function() { outerPrint.call(this, a); }
        this.innerPrint = function() {
            document.write("<p>" + a + "</p>");
        };
    }
    
    function outerPrint(a) {
        document.write("<p>" + a + "</p>");
    };
    

    Again, I assume you have a more complex function in place of outerPrint, so this way you can avoid the duplication of a large function at the expense of duplication of a minimal wrapper. This way you retain the privateness of the a variable, while a public getter would allow external code to inspect its value.

    UPDATE: On @Bergi’s remark, I’ve modified the code to make outerPrint a local function in the same scope where the obj constructor is defined. No longer being on the prototype, it will not be directly callable for obj instances. Note that all of the code will need to be inside a function scope, in order to avoid a global outerPrint function.

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

Sidebar

Related Questions

Edited Post These are the new functions that I have created using your template
I thought I would be smart and create member functions that accepted output iterators.
I thought that in principle Haskell's type system would forbid calls to impure functions
I thought that a delegate instance was interchangeable with a function instance. Take the
So here's the problem. We have some big dojo forms created using Zend_Dojo_Form. The
I know that the prototype property of JavaScript function objects is copied to the
I have a .NET DLL containing functions that I can use in PHP. Though
I thought that by convention, only methods with an exclamation mark altered the object.
I thought that a 32-bit process could use at most 2GB RAM, since half
I thought that by default my Regex would exhibit the greedy behavior that I

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.