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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:15:49+00:00 2026-05-30T20:15:49+00:00

I am new to oop in Javascript and just need to get to grips

  • 0

I am new to oop in Javascript and just need to get to grips with this.

If I have a class with properties

function myClass(){
this.foo=null;
}

I then use inheritance to create a child class

myChild.prototype = new myClass(); 

function myChild(){
alert(this.foo);
}

How do I set the property of foo when instantiating the child class e.g I want to alert ‘bar’. I don’t want to simply pass ‘bar’ to myChild as I have a list of properties to set that are relevant to a method in myClass and not myChild.

var newChild = new myChild();
  • 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-30T20:15:50+00:00Added an answer on May 30, 2026 at 8:15 pm

    You can actually find the answer to this question also in my answer to previous question and it is similar to inheritance in other languages.

    If you extend a class, the constructor function of the child class has to accept its own arguments and the one for the parent class. So assuming you have:

    function Parent(a) {
        this.foo = a;
    };
    
    // and 
    
    function Child(b, a) {
        Parent.call(this, a); // executes the parent constructor for this instance
        this.bar = b;
        alert(this.foo);
    };
    
    inherits(Parent, Child);
    

    (the implementation of inherits can be found in this answer).

    Inside Child you have to call the construtor of the parent class and pass the parameters, similar how you do it in Java or Python.

    If you have many parameters, then you can make use of the arguments object, to make things a bit easier:

    function Parent(a, b, c, d) {...};
    
    function Child(e, f) {
       // c and d are parameters for `Child`
       // arguments[0] == e
       // arguments[1] == f
       // all other arguments are passed to Parent, the following
       // creates a sub array arguments[2..n]
       Parent.apply(this, [].slice.call(arguments, 2); 
       /...
    }
    
    // later
    
    var child = new Child(e, f, a, b, c, d);
    

    In general, myChild.prototype = new myClass(); is not a good inheritance pattern as most of the time, classes expect some arguments. This won’t execute the parent constructor for each instance but only once for all instances.

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

Sidebar

Related Questions

New to OOP with Perl, and just had a quick question. I have this
I am new to OOP Javascript and am having trouble with the this keyword
I am new to OOP and I have been working on this example but
I just started with OOP on Javascript. I am new to programming world. Could
I am new to JavaScript OOP. Can you please explain the difference between the
I'm new to the OOP paradigm, so there's probably a simple explanation for this
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
New to javascript/jquery and having a hard time with using this or $(this) to
I'm relatively new to using oop in Javascript, and I'm wondering what the best
Recently I saw the following code that creates a class in javascript: var Model.Foo

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.