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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:40+00:00 2026-06-14T21:29:40+00:00

function A() { this.a = ‘this is a’; var b = ‘this is b’;

  • 0
function A() {
    this.a = 'this is a';
    var b = 'this is b';
}

function B() {
    var self = this;
    this.c = 'this is c';
    var d = 'this is d';

    // a: undefined, b: undefined, c: this is c, d: this is d
    $("#txt1").text('a: ' + A.a + ', b: ' + b + ', c: ' + this.c + ', d: ' + d);

    C();

    function C() {
        // this.c is not defined here
        // a: undefined, b: undefined, c: this is c, d: this is d
        $("#txt2").text('a: ' + A.a + ', b: ' + b + ', c: ' + self.c + ', d: ' + d);
    }
}
B.prototype = new A();

var b = new B();
​

Is it possible for class B and inner function C to get variable a and b?

Fiddle file is here: http://jsfiddle.net/vTUqc/5/

  • 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-14T21:29:44+00:00Added an answer on June 14, 2026 at 9:29 pm

    You can get a in B, using this.a, since the prototype of B is an instance of A. You can also get a in C, using self.a:

    function A() {
        this.a = 'this is a'; // This is accessible to any instance of A
        var b = 'this is b';  // This is not accessible outside the scope of the function
    }
    function B() {
        var self = this;
    
        alert(this.a); // Alerts 'this is a'
    
        C(); // Also alerts 'this is a'
    
        function C() {
            alert(self.a);
        }
    }
    B.prototype = new A();
    new B();
    

    You can’t get b directly on the other hand. If you want to access it, you could use a function that returns its value:

    function A() {
        this.a = 'this is a';
        var b = 'this is b';
        this.returnb = function(){
            return b;
        }
    }
    

    Now b is accessible to instances of A via (new A()).returnb()

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

Sidebar

Related Questions

$.Comment = function() { this.alertme = Alert!; } $.Comment.prototype.send = function() { var self
I have this two methods : Cluster.prototype.initiate_xhr_request = function(url, callback) { var self =
See this code: var MyObject = new function() { this.tos = new Array(); this.show
var a = function(){ this.x = function(){alert('apple');} this.y = function(callback){ if(typeof callback == 'undefined'
var CoreGroups = new function(){ this.name = function(){return 'name test'}; var functionName = function(){
I see this throughout our codebase: (function(undefined) { var foo = { dirtyRow: undefined,
I have this little script: var moolang = new Class({ initialize: function(element) { this.el
I have this: var Test = new function() { this.init = new function() {
var foo = function () { this.bar = 1; } >> foo.bar undefined How
var Box = function(){ this.parm = {name:rajakvk,year:2010}; Box.prototype.jspCall = function() { $.ajax({ type: post,

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.