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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:22:03+00:00 2026-05-28T00:22:03+00:00

Considering the fallowing example : function A(obj) { } B.prototype = new A; B.prototype.constructor

  • 0

Considering the fallowing example :

function A(obj) {
}

B.prototype = new A;
B.prototype.constructor = B;

function B(obj) {
    A.call(this, obj);
}

where B should inherit the prototype from A. Is this code correct? Why is it that the function A is called once when the script is parsed, without any instance from A or B being declared? Is it because of the fallowing line?

B.prototype = new A;

If so, how can B inherit A without calling function A in the definition.

  • 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-28T00:22:04+00:00Added an answer on May 28, 2026 at 12:22 am

    You can avoid calling A again if you use Object.create

    B.prototype = Object.create(A.prototype)
    

    Object.create creates a new object that has the given parameter as its prototype (the actual prototype, not the “prototype” property). It is not present in old browsers (IE < 8, FF < 4) but you can (for our purposes) create your own version if you want to. The basic idea is precisely creating a version of A that does nothing (and therefore can be called wihthout undesired side effects)

    if(!Object.create){
        Object.create = function(proto){
            var F = function(){};
            F.prototype = proto;
            return new F(); //BTW, always use parenthesis when calling new. It looks better.
        }
    }
    

    Another thing you can do is never put logic inside the constructor function and instead put it in a separate init method that must be called afterwards.

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

Sidebar

Related Questions

Considering following code public class A { public static void main(String[] args) { new
Considering this code, can I be absolutely sure that the finally block always executes,
I've searched everywhere for a solution to this...even Google's own code example doesn't work.
Considering the following javascript example: var myobj = { func1: function() { alert(name in
Take for example the following controller/action: public function indexAction() { return $this->render('TestBundle:TestController:index.html.twig'); } I
Considering this class Class 1 class myclass { public static function myfunction($condition, $string) {
Considering this example as a base example, I created the application but when I
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
Considering a path such as var dir = new File(c:\test\project1); How do I easily
Considering following code: class Results { public int playerId; public int score; public int

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.