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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:47:07+00:00 2026-05-29T16:47:07+00:00

I am studying the accessibility to variables from methods inherited from a parent to

  • 0

I am studying the accessibility to variables from methods inherited from a parent to a child object.

The inheritance is performed associating an instance of the parent to the prototype of the child’s constructor:

Function.prototype.extend = function(child) {
    var prototype = new this;

    child.prototype = prototype;
    return child;
};

So I define a parent class:

var Parent = function() {
    var self = this;

    this.init = function() {
        self.name = 'Abram';
    };
    this.getName = function() {
        alert('self.name = ' + self.name);
    };
    this.init();
};

and a child class:

var Baby = Parent.extend(function(name) {
    var self = this;

    this.init = function() {
        self.name = name;
    };
    this.init();
});

// instantiate a child object:
var baby = new Baby('Jimmy');

// call the inherited method:
baby.getName(); // undefined!

The method getName is correctly inherited, but it has no access to the self.name member.
How come, if it is available in the scope of the child class instance?

Checking the body of the method with alert(baby.getName) I get:

function () {
    alert("self.name = " + self.name);
}

which is exactly what I expected.
But why it does not resolve the self variable locally?

  • 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-29T16:47:08+00:00Added an answer on May 29, 2026 at 4:47 pm

    The variable “self” in the parent “getName()” function refers to the parent instance. The “init” function in the child sets the name on that object, not the prototype object.

    Thus:

    1. var baby = new Baby("Jimmy"); results in an object with a “name” property set to the string “Jimmy”
    2. baby.getName(); calls the function on the prototype object, since there’s no “getName” property on the “baby” object. Inside that function, a reference is made to the variable “self”, which refers to that prototype object — not to the object “baby”. There’s no “name” property set on the prototype object.

    See what happens if you change the .getName() function in the prototype to use this.name instead of self.name.

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

Sidebar

Related Questions

Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
While studying the Collection API, we find that some methods ( add , remove
studying some sample code from an iOS programming course (cs193p fall2010) i came across
I am new to studying jquery.ajax. I want to paste some data from a.php
I'm studying for an exam, and this is a problem from an old test:
when studying RMI sometimes (head first Java) dudes use Naming.rebind(name, object) but other peoples
While studying how to build a nested table from list elements in CSS, I
Studying C#, my books are showing me classes for readin files. I've found 2
Studying compilers course, I am left wondering why use registers at all. It is
Studying MS Exam 70-536 .Net Foundation I've got to Chapter 11 Application Security and

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.