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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:23:42+00:00 2026-05-18T11:23:42+00:00

Quite recently I read about JavaScript call usage in MDC https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call one linke of

  • 0

Quite recently I read about JavaScript call usage in MDC

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call

one linke of the example shown below, I still don’t understand.

Why are they using inheritance here like this

Prod_dept.prototype = new Product();

is this necessary? Because there is a call to the super-constructor in

Prod_dept()

anyway, like this

Product.call

is this just out of common behaviour? When is it better to use call for the super-constructor or use the prototype chain?

function Product(name, value){
  this.name = name;
  if(value >= 1000)
    this.value = 999;
  else
    this.value = value;
}

function Prod_dept(name, value, dept){
  this.dept = dept;
  Product.call(this, name, value);
}

Prod_dept.prototype = new Product();

// since 5 is less than 1000, value is set
cheese = new Prod_dept("feta", 5, "food");

// since 5000 is above 1000, value will be 999
car = new Prod_dept("honda", 5000, "auto");

Thanks for making things clearer

  • 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-18T11:23:42+00:00Added an answer on May 18, 2026 at 11:23 am

    The answer to the real question is that you need to do both:

    • Setting the prototype to an instance of the parent initializes the prototype chain (inheritance), this is done only once (since the prototype object is shared).
    • Calling the parent’s constructor initializes the object itself, this is done with every instantiation (you can pass different parameters each time you construct it).

    Therefore, you should not call the parent’s constructor when setting up inheritance. Only when instantiating an object that inherits from another.

    Chris Morgan’s answer is almost complete, missing a small detail (constructor property). Let me suggest a method to setup inheritance.

    function extend(base, sub) {
      // Avoid instantiating the base class just to setup inheritance
      // Also, do a recursive merge of two prototypes, so we don't overwrite 
      // the existing prototype, but still maintain the inheritance chain
      // Thanks to @ccnokes
      var origProto = sub.prototype;
      sub.prototype = Object.create(base.prototype);
      for (var key in origProto)  {
         sub.prototype[key] = origProto[key];
      }
      // The constructor property was set wrong, let's fix it
      Object.defineProperty(sub.prototype, 'constructor', { 
        enumerable: false, 
        value: sub 
      });
    }
    
    // Let's try this
    function Animal(name) {
      this.name = name;
    }
    
    Animal.prototype = {
      sayMyName: function() {
        console.log(this.getWordsToSay() + " " + this.name);
      },
      getWordsToSay: function() {
        // Abstract
      }
    }
    
    function Dog(name) {
      // Call the parent's constructor
      Animal.call(this, name);
    }
    
    Dog.prototype = {
        getWordsToSay: function(){
          return "Ruff Ruff";
        }
    }    
    
    // Setup the prototype chain the right way
    extend(Animal, Dog);
    
    // Here is where the Dog (and Animal) constructors are called
    var dog = new Dog("Lassie");
    dog.sayMyName(); // Outputs Ruff Ruff Lassie
    console.log(dog instanceof Animal); // true
    console.log(dog.constructor); // Dog
    

    See my blog post for even further syntactic sugar when creating classes. http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html

    Technique copied from Ext-JS and http://www.uselesspickles.com/class_library/ and a comment from https://stackoverflow.com/users/1397311/ccnokes

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

Sidebar

Related Questions

Recently, I've read quite a few articles about Minitest. I really like the idea
Recently I read some articles about some doubts about benefits of acceptance testing, because
I've been working with UDP sockets quite a lot recently. I've read that UDP
I recently found out about java.util.Properties , which allows me to write and read
I recently read about Dynamic Creation as one of the design pattern in Cocoa.
I quite recently learned about the C++ classes friend keyword and the uses in
I recently imported quite a few posts (many thousands) into WordPress and found out
Recently I spent quite some time writing various Visual Studio Extensions projects. Even though
I recently found that dynamically creating object and methods in Ruby is quite a
i am quite familiar with Smarty and recently decided to use Zend framework in

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.