Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are some JavaScript constructors not functions?
    primarykey
    data
    text
    <p>Clarification:<br> "JavaScript constructor" should be more properly be written as "javascript constructor" to emphasize that the constructors considered are not just the native JavaScript language constructors, such as Object, Array, Function, etc. but also others, extrinsic to the JavaScript language definition but intrinsic to a browser, such as <code>XMLHttpRequest</code>, The word "JavaScript" is meant to indicate these constructors are expressed and accessed using JavaScript.</p> <p>some references: </p> <ul> <li>Using a Constructor Function in <a href="https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Using_a_Constructor_Function" rel="nofollow noreferrer">Working with Objects - MDN Docs</a></li> <li>"<code>constructor</code><br> Specifies the function that creates an object's prototype"<br> in <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object#Properties_2" rel="nofollow noreferrer">Object - MDN Docs</a></li> <li>an example: "... calling the <code>Worker()</code> constructor ..."<br> <a href="https://developer.mozilla.org/en/DOM/Worker" rel="nofollow noreferrer">Worker - MDN Docs</a></li> <li><a href="https://stackoverflow.com/questions/6961019/what-are-the-predefined-primitive-constructors">What are the predefined primitive constructors?</a></li> <li><a href="https://stackoverflow.com/questions/6936071/where-are-constructors-such-as-new-image-and-new-option-documented">Where are constructors such as, `new Image()` and `new Option()`, documented?</a></li> <li><a href="https://stackoverflow.com/questions/5424571/where-is-the-documentation-for-all-of-the-javascript-html-element-constructors">Where is the Documentation for all of the Javascript HTML Element Constructors?</a></li> </ul> <p>Rhetorically, there are references to constructor <em>functions</em> but NOT constructor <em>objects</em>!</p> <p>(Facetiously, this is because Objects ARE functions, and Functions are objects!<br> <a href="https://stackoverflow.com/questions/372202/why-in-javascript-is-a-function-considered-both-a-constructor-and-an-object">Why in JavaScript is a function considered both a constructor and an object?</a><br> More specifically, objects, or is that obj-eggs?, ARE, ignoring literal instances, instantiations of functions and functions are Object instances of Functions. It is arguable that functions are fundamental to the existence of objects as evidenced by the fact<br> 7. <a href="https://developer.mozilla.org/en/JavaScript/Guide/Functions" rel="nofollow noreferrer">Functions</a><br> &nbsp; &nbsp; &nbsp; precedes<br> 8. <a href="https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects" rel="nofollow noreferrer">Working with Objects</a><br> in the MDN docs <a href="https://developer.mozilla.org/en/JavaScript/Guide" rel="nofollow noreferrer">JavaScript Guide</a>. That section 8, I object!, provides the details needed to create objects using constructors and function instantiations!)</p> <p>Why are constructors that interface the DOM not functions?</p> <pre><code>javascript: alert([ "using browser environment: \n"+window.navigator.userAgent, Option, Image, Audio, Storage, XMLHttpRequest, Worker, FileReader, ] . join("\n\n")); </code></pre> <p>shows us:</p> <blockquote> <p>using browser environment:<br> Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3</p> <p>[object Option]</p> <p>[object Image]</p> <p>[object Audio]</p> <p>[object Storage]</p> <p>[object XMLHttpRequest]</p> <p>[object Worker]</p> <p>[object FileReader]</p> </blockquote> <p>but ... </p> <pre><code>javascript: alert([ XPCNativeWrapper, ].join("\n\n")); </code></pre> <p>(which produces</p> <blockquote> <p>function XPCNativeWrapper() { [native code] }</p> </blockquote> <p>)</p> <p>and JavaScript language <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects#Constructors" rel="nofollow noreferrer">constructors</a> ARE functions.</p> <pre><code>javascript: alert([ "using browser environment: \n"+window.navigator.userAgent, Array, Boolean, Date, Function, Number, Object, RegExp, String, Error, Iterator, ].join("\n\n")); </code></pre> <p>gives us:</p> <blockquote> <p>using browser environment:<br> Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3</p> <p>function Array() { [native code] }</p> <p>function Boolean() { [native code] }</p> <p>function Date() { [native code] }</p> <p>function Function() { [native code] }</p> <p>function Number() { [native code] }</p> <p>function Object() { [native code] }</p> <p>function RegExp() { [native code] }</p> <p>function String() { [native code] }</p> <p>function Error() { [native code] }</p> <p>function Iterator() { [native code] }</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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