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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:02:04+00:00 2026-05-16T15:02:04+00:00

I have a Javascript class defined like so: var Welcomer = function(name) { var

  • 0

I have a Javascript “class” defined like so:

var Welcomer = function(name) {
  var pName = name;
  var pMessage = function() {
    return "Hi, " + pName + "!";
  };

  return {
    sayHi: function() {
      alert(pMessage());
    }
  };
};

new Welcomer('Sue').sayHi();

Is there a way to “subclass” Welcomer in such a way that I can redefine the public methods and have access to the private methods and variables? The following will give me access to the public methods, but not to the private ones:

var UnhappyWelcomer = function(name) {
  var pSadMessage = function() {
    // won't work, b/c I don't have access to pMessage
    return pMessage() + " Miserable day, innit?";
  };

  return {
    sayHi: function() {
      alert(pSadMessage());
    }
  };
};
UnhappyWelcomer.prototype = Welcomer(); // failed attempt at inheritance

new UnhappyWelcomer().sayHi();
  • 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-16T15:02:05+00:00Added an answer on May 16, 2026 at 3:02 pm

    The simple answer to your question is No.

    You can’t do anything to gain access to those var defined things, unless your function is in the same scope, or you somehow ‘make public’ the information (by returning it, or setting it on this).

    If you have access to edit the original function though, you could rewrite things in such a way that those functions could be “passed” into an extending function, which can alter the “protected” scope of the object. The following code should give a good idea of what I am proposing.

    var Welcomer = function(name) {
      var _protected = {
        name: name,
        message: function() {
          return "Hi, " + _protected.name + "!";
        }
      };
    
      return {
        extendWith: function(extender) {
          extender.call(this, _protected);
          return this;
        },
        sayHi: function() {
          alert(_protected.message());
        }
      };
    };
    
    var UnhappyWelcomer = function(name) {
      var self = Welcomer(name).extendWith(function(_protected) {
        _protected.sadMessage = function() {
           return _protected.message() + " Miserable day, innit?";
        };
        // extending the public while still having scope to _protected...
        this.sayHi = function() {
          alert(_protected.sadMessage());
        };
      });
      return self;
    };
    
    UnhappyWelcomer('bob').sayHi();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a Javascript class defined and instantiated like this: Demo = function()
I have the following class in javascript: function User(aJid){ this.jid = aJid; this.name =
I have Javascript object like: var data = { Id: 1, Name: Some name,
I have in my javascript these 2 functions classes: // product class function Product()
I have a function in javascript that changes a span class when I click
I have begun writing my 'class' type JavaScript functions like the Module Pattern or
I have a javascript function (class) that takes a function reference as one paremter.
How would one return a class computed CSS property/property array? Like, if I have
I have some javascript that goes out and fetches a javascript class on another
I have a javascript interface class to receive some values from javascript. I toasted

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.