Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>WeatherWidget.prototype = new Widget; </code></pre> <p>does create a new instance of the <code>Widget</code> constructor and use it as <code>WeatherWidget</code>'s prototype object. Using the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/new" rel="nofollow noreferrer"><code>new</code> keyword</a> creates the new object, sets up the inheritance chain of it to <code>Widget.prototype</code>, and applies the constructor function on it (where you can set up individual properties'n'methods, or create private-scoped variables).</p> <p>Without the <code>new</code> keyword it would be an assignment of the <code>Widget</code> function to the <code>prototype</code> property - which does not make any sense. If you'd add the optional brackets (i.e. <code>Widget()</code>), it would invoke the function normally, but not as a constructor on a new instance, but with the global object as context. See also the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this" rel="nofollow noreferrer">reference for the <code>this</code> keyword</a>.</p> <p>Notice that you should not really use this code. As said, it creates a new instance by invoking the constructor function. But the purpose is only to create an <em>empty</em> object that inherits from the <code>Widget</code>s prototype object, not to instantiate something (which could do some harm, depending on the code). Instead, you should use <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create" rel="nofollow noreferrer"><code>Object.create</code></a> (or its <a href="https://stackoverflow.com/q/10141086/1048572">popular shim</a>):</p> <pre><code>WeatherWidget.prototype = Object.create(Widget.prototype); </code></pre> <p>see also <a href="https://stackoverflow.com/q/11812648/1048572">Javascript basic inheritance vs Crockford prototypical inheritance</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload