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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:43:37+00:00 2026-06-01T05:43:37+00:00

I am experimenting with JavaScript Inheritance. Basically, I am following this tutorial. I see

  • 0

I am experimenting with JavaScript Inheritance. Basically, I am following this tutorial.

I see that, with the code there, the Person class is instantiated twice. Please have a look at this fiddle.

What I did is comment out:

Person.call(this)

And the inheritance is working just fine.

In the original code, the line

Person.call(this)

is used. Is there a need of calling parent constructor with child scope?

Could you please also give some explanation, I am new to OO JavaScript.

Thanks a lot.

EDIT:

My code in the fiddle is as follows:

function Person(gender) {
    this.gender = gender;
    document.write('Person instantiated</br>');
}

Person.prototype.walk = function(){
    document.write("is walking</br>");
};

Person.prototype.sayHello = function(){
    document.write("Hello</br>");
};

Person.prototype.sayGender = function(){
    document.write(this.gender + "</br>");
};



function Student() {
    //Person.call(this);
    document.write('Student instantiated</br>');        
}
Student.prototype = new Person();

Student.prototype.constructor = Student;

Student.prototype.sayHello = function(){
    document.write("Student says Hello</br>");
}
Student.prototype.sayGoodBye = function(){
    document.write("Student says goodbye</br>");
}


var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();

document.write(student1 instanceof Person);
document.write("</br>");
document.write(student1 instanceof Student);
  • 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-06-01T05:43:39+00:00Added an answer on June 1, 2026 at 5:43 am

    Running the example you provided only calls the Person() constructor once during initial execution of the script when the line “Student.prototype = new Person();” is executed.

    If we modify your script to create a second student and separate the setup from the instantiation: bit: http://jsfiddle.net/anacW/

    function Person(gender) {
        this.gender = gender;
        document.write('Person instantiated</br>');
    }
    
    Person.prototype.walk = function(){
        document.write("is walking</br>");
    };
    
    Person.prototype.sayHello = function(){
        document.write("Hello</br>");
    };
    
    Person.prototype.sayGender = function(){
        document.write(this.gender + "</br>");
    };
    
    
    
    function Student() {
        //Person.call(this);
        document.write('Student instantiated</br>');        
    }
    Student.prototype = new Person();
    
    Student.prototype.constructor = Student;
    
    Student.prototype.sayHello = function(){
        document.write("Student says Hello</br>");
    }
    Student.prototype.sayGoodBye = function(){
        document.write("Student says goodbye</br>");
    }
    
    document.write("*** Building student1 *** </br>");
    var student1 = new Student();
    student1.sayHello();
    student1.walk();
    student1.sayGoodBye();
    
    document.write("*** Building student2 ***</br>");
    var student2 = new Student();
    student2.sayHello();
    student2.walk();
    student2.sayGoodBye();
    
    document.write("*** InstanceOf Tests ***</br>");
    document.write("student1 is Person?: " + (student1 instanceof Person));
    document.write("</br>");
    document.write("student1 is Student?: " + (student1 instanceof Student));
    document.write("</br>");
    document.write("student2 is Person?: " + (student2 instanceof Person));
    document.write("</br>");
    document.write("student2 is Student?: " + (student2 instanceof Student));
    

    ​

    This code gives:

    Person instantiated
    *** Building student1 *** 
    Student instantiated
    Student says Hello
    is walking
    Student says goodbye
    *** Building student2 ***
    Student instantiated
    Student says Hello
    is walking
    Student says goodbye
    *** InstanceOf Tests ***
    student1 is Person?: true
    student1 is Student?: true
    student2 is Person?: true
    student2 is Student?: true
    

    Which shows you that the Person constructor is only being called once, and is never called by instantiating a Student. This might be desirable in your case (I don’t know enough javascript to tell you whether it’s ‘proper’ form or not).

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

Sidebar

Related Questions

I am experimenting with HTML and JavaScript. The following code should print something when
i've started learning about javascript closures, and while experimenting, i realised that the following
i've been experimenting with javascript's prototypal inheritance and have come across something that perhaps
I was experimenting with inheritance in javascript, and wrote those two functions: Object.prototype.inherits=function(obj){this.prototype=new obj;}
I'm experimenting on this code that I got from the net (I'm trying to
I'm doing some experimenting with this malicious JavaScript line: var undefined = true; Every
While experimenting with this question on collections in Spring.NET , I discovered that Spring
I'm new to Javascript/Jquery and PHP and I'm experimenting with it. Basically, I've created
I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going
I've been experimenting with using Javascript object literal notation vs functions with prototypes and

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.