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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:26:30+00:00 2026-05-19T22:26:30+00:00

This is the extend function from the book Pro JavaScript Design Patterns function extend(subClass,

  • 0

This is the extend function from the book “Pro JavaScript Design Patterns”

function extend(subClass, superClass) {
    var F = function() {};
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = subClass;
    subClass.superclass = superClass.prototype;
    if(superClass.prototype.constructor == Object.prototype.constructor) {
       superClass.prototype.constructor = superClass;
    }
}

I have problems with the first 3 lines… It creates an empty function and then it sets the F.prototype to superClass.prototype which means (for 2 new constructor functions e.g. foo, bar and foo extends bar) that F.prototype will have a constructor property: bar and proto :Object, or not?
And on line 3: subClass.prototype = new F(); happens something that I cannot understand.. Why the inheritance happens here when F’s [[Prototype]] is Object?


What are the differences between the first 3 lines and

subClass.prototype = new superClass();

when the code executes? I mean how The first does the same as the second one.


Just to add there is a call to the superClass constructor in the subClass.
The call is “className”.superclass.constructor.call(this);

  • 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-19T22:26:30+00:00Added an answer on May 19, 2026 at 10:26 pm

    We’ve got constructors:

    F , Super & Sub.

    We’ve got objects f , super, & sub that are made using said constructors.

    i.e. f = new F , super = new Super , sub = new Sub

    We know f.__proto__ === super.__proto__ === Super.prototype from line 2

    From line 3 we can see that sub.__proto__ === f & sub.__proto__.__proto__ === Super.prototype

    Also we have Sub.superClass === Super.prototype from line 5.

    and from line 4 & 6 we can say that sub.constructor === Sub & super.constructor === Super

    The reason we can call new F before on line 3 before line 7 is because line 7 set’s

    f.__proto__.constructor === Super where as f.constructor is already Sub. basically line 7 is cleaning Super up and shouldn’t effect Sub at all because you should never call .__proto__.constructor in real code.

    This particular function explicity does not call Super as function but only make’s sure that objects constructed through Sub have Super.prototype in their chain.

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

Sidebar

Related Questions

Im new in OOP programming and C++ and Im currently into learning design patterns.
So I want to extend for example this RecursiveIterator from the SPL with the
I have found this example on StackOverflow: var people = new List<Person> { new
With C# 3.0, I know you can extend methods using the 'this' nomenclature. I'm
Being relatively new to functional programming, I expend lots of energy wondering is this
This is my first post here and I wanted to get some input from
I'm having some trouble understanding the IF clause at the end of this function
I am currently reading the book called "Programming ActionScript 3.0" available from Adobe. After
I am writing some objects (classes I guess) in javascript. Class B inherits from
the urlize function from django.utils.html converts urls to clickable links. My problem is that

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.