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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:48:27+00:00 2026-05-16T22:48:27+00:00

My problem is with objInfo(). How can I return an object via a passed-in

  • 0

My problem is with objInfo(). How can I return an object via a passed-in variable? I’m trying to namespace my code and use private/public vars.

Bonus Q: How would you otherwise improve the code?

// Namespace all my code
var bab = new function() {

    // Declare cat object
    function cat()
    {
      this.eyes = 2;
      this.legs = 4;
      this.diet = 'carnivore';

      return true;
    }

    // Declare lion object
    function lion()
    {
      this.mane = true;
      this.origin = 'Africa';
      this.diet = 'people'; // has priority over cat's diet

      return true;
    }

    // Make lion a subclass of cat
    lion.prototype = new cat();

    // Create an instance of class lion
    var simba = new lion();

    // Share diet publicly
    this.objInfo = function(name) {
        return name; // simba works, name doesn't
    };

};

alert(bab.objInfo('simba').diet);

Note: Source is sampled from various places

  • 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-16T22:48:28+00:00Added an answer on May 16, 2026 at 10:48 pm

    Other than namespacing, it’s not clear to me what you’re trying to do, but I’ve included a code review of sorts under the divider below. More high-level comments first.

    There are a couple of issues here. First off, you almost never want to write new function() { }. It’s a very advanced technique that’s easy to get wrong (and very easy for anyone doing maintenance on the code to misunderstand). There’s an example of another, less-confusing way to get the same effect (plus some other benefits) below.

    Here’s an example of a namespaced module providing two “classes”, Cat and Lion (I’ve made them initially-capped because that’s the usual convention: initial caps on constructor functions and initial lower case on non-constructor functions, just to make it easy to read code):

    var Animals = (function() {
        var publics = {};
    
        // A Cat
        publics.Cat = Cat;
        function Cat() {
            this.eyes = 2;
            this.legs = 4;
            this.diet = 'carnivore';
        }
    
        // A Lion
        publics.Lion = Lion;
        function Lion() {
            this.mane = true;
            this.origin = 'Africa';
            this.diet = 'people'; // has priority over cat's diet
        }
        Lion.prototype = new Cat();
    
        // Return our public symbols
        return publics;
    })();
    
    // Usage
    var l = new Animals.Lion();
    alert(l.eyes); // alerts "2" (inherited from Cat)
    alert(l.diet); // alerts "people" (overridden by Lion)
    

    (You can, of course, call publics anything else you want — pubs, p, whatever. It’s the equivalent of this in the outermost layer of your new function() { } function, but less confusing.)

    But just replacing the prototype on Lion is a bit simplistic. When you start getting into subclassing, there are several other things you want to look at doing. Here’s a blog post that details a fairly complete means of building classes, including subclassing, calling superclass functions, etc.

    In terms of looking something up by string, you can do that with brackets notation on any object:

    var obj = {};
    obj.foo = 42;
    alert(obj["foo"]); // alerts "42" by retrieving the property "foo" from `obj`
    var x = "f" + "o" + "o";
    alert(obj[x]);     // alerts "42" by retrieving the property "foo" from `obj`
    

    Code review follows.


    Here’s a code review:

    // Namespace all my code
    // [TJC] Use the (function() { ... })(); mechanism described above rather than
    // `new function() { ... }`, which is fairly confusing to the reader and troublesome
    // to use inside inner functions (see below)
    var bab = new function() {
    
        // Declare cat object
        // [TJC] Convention is to use initial caps for constructor functions,
        // e.g. "Cat" not "cat"
        function cat()
        {
          this.eyes = 2;
          this.legs = 4;
          this.diet = 'carnivore';
    
          // [TJC] Don't return anything out of constructor functions
          return true;
        }
    
        // Declare lion object
        // [TJC] "Lion" rather than "lion" would be more conventional
        function lion()
        {
          this.mane = true;
          this.origin = 'Africa';
          this.diet = 'people'; // has priority over cat's diet
    
          // [TJC] Don't return anything out of constructor functions
          return true;
        }
    
        // Make lion a subclass of cat
        // [TJC] There are several other things you want to consider in
        // addition to replacing the prototype
        lion.prototype = new cat();
    
        // Create an instance of class lion
        // [TJC] From your usage below, it looks like you
        // want to be able to look up "simba" using a string
        // later. So use the below rather than this commented-out
        // line:
        //var simba = new lion();
        var instances = {};           // [TJC]
        instances.simba = new lion(); // [TJC]
    
        // Share diet publicly
        // [TJC] You don't need a function for this at all, just
        // expose "instances" directly. But if you want it:
        this.objInfo = function(name) {
                // [TJC] To look up something by name using a string,
                // use brackets:
            //return name; // simba works, name doesn't
                return instances[name]; // [TJC]
        };
    
    };
    
    alert(bab.objInfo('simba').diet);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to solve the following question which i can't get to work by
I would like to update my SQL lite database with the native update-method of

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.