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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:45:04+00:00 2026-06-11T21:45:04+00:00

What does the following code do: WeatherWidget.prototype = new Widget; where Widget is a

  • 0

What does the following code do:

WeatherWidget.prototype = new Widget;

where Widget is a constructor, and I want to extend the Widget ‘class’ with a new function WeatherWidget.

What is the new keyword doing there and what would happen if it is left out?

  • 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-11T21:45:05+00:00Added an answer on June 11, 2026 at 9:45 pm
    WeatherWidget.prototype = new Widget;
    

    The new keyword calls Widget as a constructor and the return value is assigned to the prototype property. (If you would omit new, you would not call Widget unless you added an argument list, (). However, calling Widget that way might not be possible. It would certainly have the potential to spoil the global namespace if it is not strict mode code and the implementation is conforming to ECMAScript Ed. 5.x there, because then this in the constructor would refer to ECMAScript’s global object.)

    But this approach actually comes from a really viral bad example in the old Netscape JavaScript 1.3 Guide (mirrored at Oracle, formerly Sun).

    This way, your WeatherWidget instances will all inherit from the same Widget instance. The prototype chain will be:

    [new WeatherWidget()] → [new Widget()] → [Widget.prototype] → …
    

    This can be useful, but most of the time you would not want it to happen. You should not do that here unless you want all your WeatherWidget instances to share among them the property values they inherit from this Widget instance, and only through it, from Widget.prototype. Another problem is that you need to call the parent constructor this way, which may not allow to be called without arguments as you do, or would not initialize properly. It certainly has nothing to do with emulation of class-based inheritance as known, e.g., from Java.

    The proper way to implement class-based inheritance in these prototype-based languages is (originally devised by Lasse Reichstein Nielsen in comp.lang.javascript in 2003, for cloning objects):

    function Dummy () {}
    Dummy.prototype = Widget.prototype;
    WeatherWidget.prototype = new Dummy();
    WeatherWidget.prototype.constructor = WeatherWidget;
    

    The constructor prototype property should be fixed as well, so that your WeatherWidget instances w would have w.constructor === WeatherWidget as expected, and not w.constructor === Widget. However, be aware that it is enumerable afterwards.

    This way, WeatherWidget instances will inherit properties through the prototype chain, but will not share property values among them, because they inherit from Widget.prototype through Dummy which has no own properties:

    [new WeatherWidget()] → [new Dummy()] → [Widget.prototype] → …
    

    In implementations of ECMAScript Ed. 5 and later, you can and should use

    WeatherWidget.prototype = Object.create(Widget.prototype, {
      constructor: {value: WeatherWidget}
    });
    

    instead. This has the additional advantage that the resulting constructor property is not writable, enumerable, or configurable.

    The parent constructor will only be called if you call it explicitly, from WeatherWidget, for example with

    function WeatherWidget (…)
    {
      Widget.apply(this, arguments);
    }
    

    See also Function.prototype.extend() in my JSX:object.js for how to generalize this. Using that code, it would become

    WeatherWidget.extend(Widget);
    

    My Function.prototype.extend() takes an optional second argument with which you can easily augment the prototype of WeatherWidget instances:

    WeatherWidget.extend(Widget, {
      foo: 42,
      bar: "baz"
    });
    

    would be equivalent to

    WeatherWidget.extend(Widget);
    WeatherWidget.prototype.foo = 42;
    WeatherWidget.prototype.bar = "baz";
    

    You will still need to call the parent constructor explicitly in the child constructor, though; that part cannot reasonably be automated. But my Function.prototype.extend() adds a _super property to the Function instance which makes it easier:

    function WeatherWidget (…)
    {
      WeatherWidget._super.apply(this, arguments);
    }
    

    Other people have implemented similar extensions.

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

Sidebar

Related Questions

Why does the following code give me an error (g++ 4.1.2)? template<class A> class
What does the following code mean? $shipping_modules = new shipping($shipping);
Why does the following code trigger Expected an assignment or function call and instead
How does the following code work? public void SomeMethod() { StringBuilder sb = new
Why does the following code raise the exception shown below? BigDecimal a = new
What does the following code do? class MyClass { private int[] myPrivates; public int[]
Does the following code render the using(...) function/purpose irrelevant? Would it cause a deficiency
What does the following code do: $('#myelement').is('*') What does the asterisk signify? Since there
Why does the following code output 0 indices? I want the indices to be:
Why does the following code not work? Example: http://jsfiddle.net/ZZe5X/18/ $('.submit').submit(function () { e.preventDefault() if

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.