Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Function's __proto__?

Tags:

javascript

I see this nice diagram and I've done some tests in my Chrome browser, but I don't know how to explain this:

> Function.prototype
  function Empty() {}
> Function.__proto__
  function Empty() {}
> typeof(Empty)
  "undefined"

What is the function Empty() {}, and why Function.prototype is a function not a object just like Object.prototype?

From the diagram above, it seems everything in JavaScript starts from Object.prototype, am I right about that?

like image 739
lostyzd Avatar asked Oct 07 '11 14:10

lostyzd


People also ask

Is __ proto __ a property?

The __proto__ property of Object. prototype is an accessor property (a getter function and a setter function) that exposes the internal [[Prototype]] (either an object or null ) of the object through which it is accessed.

What is Dunder proto?

dunder proto === __proto__ Every object in JS has this property. It points back to the prototype object of the constructor function that created that object.

What is Proto in JavaScript object?

The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible. Every function includes prototype object by default. Prototype in JavaScript.

What is the prototype of console log?

log nor console are class constructors, so their prototype properties are undefined . Since undefined === undefined , console. log. prototype === console.


2 Answers

First, the function Empty() {} representation is V8 stuff.

In V8, the Function.prototype object has "Empty" as the value of the Function.prototype.name property, so I guess you are probably using the Chrome's Developer Console, and it displays the name of the function in this way.

The name property of function objects is non-standard (not part of ECMA-262), that's why we see differences between implementations.

Now, Function.prototype is a function, that returns always undefined and can accept any number of arguments, but why?. Maybe just for consistency, every built-in constructor's prototype is like that, Number.prototype is a Number object, Array.prototype is an Array object, RegExp.prototype is a RegExp object, and so on...

The only difference (for example, between any function object and Function.prototype) is that obviously Function.prototype inherits from Object.prototype.

it seems everything in javascript start from Object.prototype, am I right about that?

Well, you're right Object.prototype is the last object of the prototype chain of most objects, but in ECMAScript 5, you can even create objects that doesn't inherit from anything (just like Object.prototype is), and form another inheritance chain, e.g.:

var parent = Object.create(null),     child = Object.create(parent);  Object.prototype.isPrototypeOf(parent);  // false  Object.getPrototypeOf(parent);           // null Object.getPrototypeOf(Object.prototype); // null 
like image 124
Christian C. Salvadó Avatar answered Sep 21 '22 15:09

Christian C. Salvadó


To integrate CMS excellent answer:

it seems everything in javascript start from Object.prototype, am I right about that?

Absolutely, objects in JavaScript are chained up to the basic Object. The chain of inheritance works at runtime, so if a base object is modified, everything chained to it will inherit the modifications instantly. If an Object doesn't have a method or a property, the Javascript implementation will follow the prototype chain until it finds it or it will fail.

__proto__ is a non standard accessor to the prototype, which is supported across browsers, but not by IE. Anyway is not meant to be used by application code.

like image 45
stivlo Avatar answered Sep 22 '22 15:09

stivlo