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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:25:12+00:00 2026-05-25T12:25:12+00:00

I’m doing some experiences with OOP in JavaScript. My goal is to have a

  • 0

I’m doing some experiences with OOP in JavaScript. My goal is to have a parent object which holds methods common to several other objects, which inherit from that parent object. Thing is, I want the parent objects’ methods to be able to read the childrens’ fields.

I use the following function for inheritance:

Function.prototype.inherits=function(obj){this.prototype=new obj();}

These are some example objects:

function Genetic(c){
    this.code=c;
}
//My 'parent object':
function Animal(){
    this.getCode=function(){
        return(genetic.code);
    }
}
g=new Genetic('test');
function Dog(){
    genetic=g;
}
Dog.inherits(Animal);
g=new Genetic('foo');
function Cat(){
    genetic=g;
}
Cat.inherits(Animal);

d=new Dog();
c=new Cat();

Now, I expect d.getCode() to return 'test', and c.getCode() to return 'foo'. Problem is, both return 'foo'. The variable genetic is in the Animal scope, and not in the Dog/Cat scope. Meaning that whenever I create a new object that inherits from Animal, the genetic variable will be overridden. Proof:

function Bla(){}
Bla.inherits(Animal);
bla=new Bla()
bla.getCode() //Returns 'foo'

I can set the genetic variable to being a private variable of Dog and Cat with var:

function Dog(){
    var genetic=g;
}

Problem is, since genetic is now private to Dog, it can’t be accessed by the Animal object, rendering the whole inheritance pointless.

Do you see any way to solve that?

EDIT: Also, I want gentic to be private, so that it can’t be modified in Dog/Cat instances.

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

    The variable ‘genetic’ is in the Animal scope, and not in the Dog/Cat scope.

    No, genetic is global. There exists only one genetic variable in your whole application. Make it a property of the object.

    Furthermore, a better way of inheritance is the following:

    function inherits(Child, Parent) {
        var Tmp = function(){};
        TMP.prototype = Parent.prototype;
        Child.prototype = new Tmp();
        Child.prototype.constructor = Child;
    }
    

    Then you can have the parent constructor accept arguments and don’t have to repeat code:

    //My 'parent object':
    function Animal(g){
        this.genetic = g;
    }
    
    Animal.prototype.getCode = function() {
        return this.genetic.code;
    }
    
    function Dog(){
        Animal.apply(this, arguments);
    }
    inherits(Dog, Animal);
    
    function Cat(){
        Animal.apply(this, arguments);
    }
    inherits(Cat, Animal);
    
    var d = new Dog(new Genetic('test'));
    var c = new Cat(new Genetic('foo'));
    

    I would advice to document your code properly and rather follow a clear prototype/inheritance chain than trying to do something the language is not designed for.

    However, with the inherits function given above, you could do:

    function Animal(g){
        var genetic = g
    
        this.getCode = function(){
            return genetic.code ;
        }
    }
    

    with the rest of the code staying the same. Then you have your “private” variable at the cost of every instance having its own getCode function.

    Edit: This would not let you access genetic in any function assigned to Dog or Cat, unless you also keep a reference to the value in their constructors.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.