5

I have this function :

function fff(){}

Which is a function which is an instance of Function constructor

so fff.__proto__ should show me : function Function() { [native code] }

But it doesn't.

It shows : function Empty() {}

enter image description here

It is only at the constructor property of __proto__ that I see function Function() { [native code] }

Question :

What is this function Empty() {} function
and why fff.__proto__ won't show me : function Function() { [native code] } ?

nb I know that __proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new.

But again : function fff is a function which is instantiated behind the scenes by newing Function constructor....so ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

5

You're misunderstanding __proto__.

__proto__ returns the prototype value that the object inherits; not its constructor.

All functions (including Function itself) inherit Function.prototype.
Thus, Function.__proto__ === Function.prototype is true.
This object is specified in section 15.3.4 of the spec:

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.

See also

15.3.4.2 Function.prototype.toString ( )

An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white space, line terminators, and semicolons within the representation String is implementation-dependent.

The toString function is not generic; it throws a TypeError exception if its this value is not a Function object. Therefore, it cannot be transferred to other kinds of objects for use as a method.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • _proto is the ctor's prototype_--- that helped a lot. thanks slaks. ( i was sure that it is the constructor) – Royi Namir Oct 28 '13 at 17:11
  • @RoyiNamir: No; that's the `constructor` property (which comes from the prototype) – SLaks Oct 28 '13 at 17:12
  • I guess the _NO_ part is for my second cpart of the comment – Royi Namir Oct 28 '13 at 17:16
  • - So can you please tell me - why in this [code](http://jsbin.com/OrAGOrU/2/edit) I dont see the alert while in [this](http://jsbin.com/ULACixI/2/edit) one - i do see the alert ? if I create an instance from newing a function - it should go to the Function.prototype....no ? – Royi Namir Oct 28 '13 at 17:50
  • @RoyiNamir: No. `MyFunction.prototype` is not a function; it's an object that instances of your function will inherit. See http://es5.github.io/#x15.3.5.2 – SLaks Oct 28 '13 at 18:31
  • I didnt write `MyFunction.prototype` I wrote `Function.prototype`, and I still dont understand why the first one is not working while the other does – Royi Namir Oct 28 '13 at 18:57
  • @RoyiNamir: Your function's `prototype` (which its instances inherit) is not a function and does not inherit `Function.prototype`. – SLaks Oct 28 '13 at 19:00
  • But `new myFunc()` is an Object. and Object is a function : >> `function Object() { [native code] }` so it is an instance of Function ctor....no ? ( thank you very much for answering) – Royi Namir Oct 28 '13 at 19:02
  • @RoyiNamir: No. It inherits `Object.prototype`, not `Object`. You're confusing constructor inheritance ("X is an instance of Y") with prototype inheritance ("X's prototype is Y") – SLaks Oct 28 '13 at 19:22