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

  • SEARCH
  • Home
  • 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 7581291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:09:56+00:00 2026-05-30T18:09:56+00:00

I have following setup. method and method1 both are public methods of class Student.

  • 0

I have following setup. method and method1 both are public methods of class Student. But why ca only “method” access private variable p

​function Student() {
    var p = 10;
    this.method = function() {
        document.write(p);
    }; 
};

Student.prototype.method1 = function() {
    document.write('here');
    document.write(p);
};

var s = new Student();
s.method();
s.method1();
​

How does it make sense, I mean is it “a public method that does not have access to private members!”

  • 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-05-30T18:09:57+00:00Added an answer on May 30, 2026 at 6:09 pm

    There is no magic going on with JavaScript’s prototypal inheritance.

    Student is still a function and p is local to that function. It cannot be accessed from outside code in any way. method can access p because it is defined inside Student and therefore forms a closure, but method1 is declared outside Student‘s scope.

    Assigning a function to another function’s prototype cannot give it access to its local variables.

    Consider this example:

    var p = 41;
    
    function foo() {
        console.log(p);
    }
    
    ​function Student(){
        var p = 10;
    };
    
    Student.prototype.bar = foo;
    
    var s = new Student();
    s.bar();
    foo();
    

    You might think that because foo is invoked as an object method it could have access to the local variables, but that’s just not the case. The only value that is determined dynamically is this, it is a special keyword. All other variables are strictly defined through the scope chain.

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

Sidebar

Related Questions

I have the following setup: abstract class AParentLy{ private $a; private $b; function foo(){
I have following setup, but when I put 1024 and replace all 512 with
I have the following setup (simplified, obviously): An abstract class, with an A object
I have the following setup: A templated class SpecialModel: template<typename A, typename B> class
Within a web form I have the following jQuery setup to call a method
I have the following setup: SD card Mount Receiver (MountReceiver.java) public IntentFilter getIntentFilter() {
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
I have the following setup: Code on my local machine (OS X) shared as
I have a following setup: App - Magnolia site running under Tomcat 6.0.16 ISAPI
I have the following setup, and I need to know how to persist state.

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.