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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:14:08+00:00 2026-05-22T12:14:08+00:00

Family = function(name) { this._Name = name; } Family.prototype = { getName: function() {

  • 0
Family = function(name) {
  this._Name = name;
}

Family.prototype = {
  getName: function() {
    return this._Name;
  },
  People: function(num) {
    this._Number = num;
  }
}

Family.People.prototype = {
  clearNumber: function() {
    this._Number = 0;
  }
}

People is a nested class. Its parent class is Family.

I get the error that Family.People is undefined. Could someone correct the code above?

  • 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-22T12:14:09+00:00Added an answer on May 22, 2026 at 12:14 pm

    Working code

    // function doesn't need "new" operator
    var Family = function(name) { this._Name = name; };
    
    Family.prototype = {
        getName: function() { return this._Name; }, // missing comma
        People: function(num) {
            this._Number = num;
        }
    };
    
    // work with prototypes
    Family.prototype.People.prototype = {
        clearNumber: function() { this._Number = 0; }
    };
    

    This will work. But you have to be aware, that when you call:

    var f = new Family("Doe");
    

    f.People is just an object constructor, and not an instance of some other object. You will have to instantiate it as well like:

    f.members = new f.People(3);
    

    Sou you have a constructor within your instance which is rather confusing.

    A better approach

    So it would probably be better if you’d write your prototypes this way:

    var Family = function(name) {
        this._Name = name;
        this.getName = function() { return this._Name; };
    };
    
    Family.People = function(num) {
        this._Number = num;
        this.clearNumber = function() { this._Number = 0; };
    };
    

    This actually makes a class within a class (and not within instances). So upper lines would be called this way:

    var f = new Family("Doe");
    f.members = new Family.People(3);
    

    Drill down of f instance would look like:

    f
      _Name
      getName()
      members
        _Number
        clearNumber()
    

    Private variables

    var Family = function(name) {
        var _name = name;
        this.getName = function() { return _name; };
    };
    
    Family.People = function(num) {
        var _num = num;
        this.getNumber = function() { return _num; }
        this.clearNumber = function() { _num = 0; };
    };
    

    This way we make variables private and only accessible within so they can’t be manipulated outside. You must always use functions to manipulate them. This makes it more robust especially when there are certain business rules related to variable values.

    var f = new Family("Doe");
    f._name; // this is undefined because "_name" is private closure variable
    

    Drill down of f instance would now look more like a class object instance:

    f
      getName()
      members
        getNumber()
        clearNumber()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple jQuery function: $('#selectable1 span').live('mousedown', function() { var ff = $(this).css(font-family);
I am looking for a family of hash function F1,..Fn, where each Fi maps
Say I have public class family { public person father; public person mother; public
I already read the man page of the pidfile function family. But I don't
i have a javascript function like this: function addfamily(divName){ var newdiv = document.createElement('div'); newdiv.innerHTML
Every Christmas we draw names for gift exchanges in my family. This usually involves
I'm implementing unit tests for a family of functions that all share a number
I keep stumbling on the format specifiers for the printf() family of functions. What
Friends/family/etc ask me what I do and it always causes me pause while I
Which family should I start to learn? (Never did any programming on microcontroller)

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.