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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:57:52+00:00 2026-05-25T09:57:52+00:00

Say I have a Class : function Foo() { this.foo1 = null; this.foo2 =

  • 0

Say I have a Class:

function Foo() {
  this.foo1 =  null;
  this.foo2 = function() { return false;};
}

And I want other objects to inherit variables & functions from it.

function Bar(){}
function Baz(){}

Then instantiate my objects:

var bar = new Bar();
bar.foo1   // returns null
bar.foo2() // returns false

What’s the proper function to include Foo in Bar and Baz?


I already did Bar.prototype = new Foo(); but it seems to fail on our beloved IE (<9).

  • 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-25T09:57:53+00:00Added an answer on May 25, 2026 at 9:57 am

    If you attach all the properties to the prototype (which is preferable, at least for methods),

    function Foo() {}
    
    Foo.prototype.foo1 = null;
    Foo.prototype.foo2 = function() { return false;};
    

    then assigning the parent’s prototype to the child’s prototype is sufficient:

    function inherit(Child, Parent) {
        var Tmp = function(){};
        Tmp.prototype = Parent.prototype;
        Child.prototype = new Tmp();
        Child.prototype.constructor = Child;
    }
    
    inherit(Bar, Foo);
    

    Here we used an intermediate constructor function to “decouple” the two prototypes. Otherwise if you’d change one, you’d change the other one too (as they reference the same object). This way is actually pretty popular and used by a couple of libraries.

    If not, you have to call the parent’s constructor function inside the child’s constructor function:

    function Bar() {
        Foo.call(this);
    }
    

    This is something you always should do, to assign the properties set up in the constructor function to the current object.


    An additional remark to your way:

    Bar.prototype = new Foo();
    

    this should work (also in IE actually), but it has two major flaws:

    • All the instance properties set up in Foo will become properties shared by all Bar instances.

    • What if Foo expects some arguments that are only available when you create a Bar instance?

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

Sidebar

Related Questions

Lets say I have a class like this in Java: public class Function {
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
Say I have classes Foo and Bar set up like this: class Foo {
so.. let's say i have this C function: PyObject* Foo(PyObject* pSelf, PyObject* pArgs) {
Say I have a file Foo.php: <?php interface ICommand { function doSomething(); } class
Lets say I have these classes: class Foo { public $_data; public function addObject($obj)
Let's say I have something like this: <?php namespace Twitter; class Twitter { function
Say I have a javascript function/class called Foo and it has a property called
Say I have a Javascript class defined and instantiated like this: Demo = function()
I know python functions are virtual by default. Let's say I have this: class

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.