Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>obj01</code> is simply an object that inherits from a function object, you can't create functions in this way.</p> <p>The <a href="http://bclary.com/2004/11/07/#a-11.4.3" rel="nofollow noreferrer"><code>typeof</code></a> operator returns <code>"function"</code> only when its operand is by itself <em>callable</em>.</p> <p>There are only three valid ways to create function objects:</p> <p>Function declaration:</p> <pre><code>function name (/*arg, argn...*/) { } </code></pre> <p>Function expression:</p> <pre><code>var fn = function /*nameopt*/ (/*arg, argn...*/) { }; </code></pre> <p>Function constructor:</p> <pre><code>var fn = new Function("arg", "argn", "FunctionBody"); </code></pre> <p><strong>Edit:</strong> In response to your comment, <code>obj01</code> , is just an object, its prototype chain contains a function object, then <code>Function.prototype</code> and then <code>Object.prototype</code> but that doesn't make an object <em>callable</em>.</p> <p>Your object <em>is not</em> callable, functions are just objects, but they have some <em>special</em> internal properties that allow them to behave like that.</p> <p><strong>An object is callable <em>only if</em> it implements the internal <a href="http://bclary.com/2004/11/07/#a-11.2.2" rel="nofollow noreferrer"><code>[[Call]]</code></a> property</strong>.</p> <p>There are other internal properties that function objects have, like the <a href="http://bclary.com/2004/11/07/#a-11.2.2" rel="nofollow noreferrer"><code>[[Construct]]</code></a>, which is invoked when the <a href="http://bclary.com/2004/11/07/#a-11.2.2" rel="nofollow noreferrer"><code>new</code></a> operator is used, the <a href="http://bclary.com/2004/11/07/#a-11.2.2" rel="nofollow noreferrer"><code>[[Scope]]</code></a> property which stores the lexical environment where the function is executed, and more.</p> <p>If you try to invoke your object like if it were a function, you will have a <code>TypeError</code>, because when you make a <a href="http://bclary.com/2004/11/07/#a-11.2.3" rel="nofollow noreferrer">function call</a>, the object needs to have the <code>[[Call]]</code> internal property.</p> <p>Function objects need to have the above internal properties, and <em>the only way</em> that they can be constructed is by the three methods I mentioned early, you can see how internally functions objects are created <a href="http://bclary.com/2004/11/07/#a-13.2" rel="nofollow noreferrer">here</a>.</p>
 

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