Object.getPrototypeOf(..)
returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object. so, the below makes sense
const prototype1 = {};
const object1 = Object.create(prototype1);
console.log(Object.getPrototypeOf(object1) === prototype1); // logs true
Even this looks fine
function fn() {
}
Object.getPrototypeOf(fn) == Function.prototype // logs true
But I am kind of confused why
Object.getPrototypeOf(Object) == Function.prototype // logs true
Please explain
CodePudding user response:
Some points to consider:
Object
is a function
It can be used to convert a primitive to an object. Like Object("test")
will create a String
instance.
There is a constructor for functions
The constructor for functions is Function
. It is not commonly done, but you can create a function by calling this constructor. For example: new Function("alert('test')")
returns a function that when called will display an alert.
Functions which are created with function
statements, expressions, arrow function syntax,... also have as constructor this Function
. So for instance, eval.constructor === Function
is a true expression. The same goes for Object.constructor === Function
. You can do this for any function.
Where to find the a function's prototype object?
Like every constructor, Function
has a prototype
property. That object defines the prototype properties and methods that all functions inherit. We can find that prototype object via Function.prototype
, or we can find it by taking a Function
instance (i.e. a function), and getting its prototype object. This we do by calling Object.getPrototypeOf
on a specific function.
Equalities
And so we can see that all of the following are equal:
var a = Object.getPrototypeOf(eval);
var b = Object.getPrototypeOf(parseInt);
var c = Object.getPrototypeOf(function () {});
var d = Object.getPrototypeOf(RegExp);
var e = Object.getPrototypeOf(Object);
var f = Function.prototype;
// All true:
console.log(a === f, b === f, c === f, d === f, e === f);
CodePudding user response:
Everything in JavaScript inherits from Object.prototype
. That is why everything in JavaScript is said to be objects. Even functions and arrays are objects under the hood.
So Object.prototype
is the prototype of:
Function.Prototype
Array.Prototype
Function.Prototype
is the prototype of Function
and Array
:
Array.prototype.prototype == Function.Prototype // true
Function.prototype.prototype == Function.Prototype // true
And again, Array
and Function
inherits from Object.prototype
:
Object.getPrototypeOf(Array.prototype) == Object.prototype // true
Object.getPrototypeOf(Function.prototype) == Object.prototype // true
So to return to your question. The prototype of Object
is Object.prototype
. And Object.prototype
is the prototype of Function.prototype
, thus:
Object.getPrototypeOf(Object) == Function.prototype // true
JavaScript is like a family tree:
Object.prototype
had a baby called Function.prototype
. Function.prototype
then had babies of its own called Array and Function. And then follows arrays, classes etc... In the end, everything leads back to Object
CodePudding user response:
Syntax:
Object.getPrototypeOf(obj)
Parameters
obj
The object whose prototype is to be returned.
Return value
The prototype of the given object. If there are no inherited properties, null is returned.
For Example:
Using getPrototypeOf
const proto = {};
const obj = Object.create(proto);
Object.getPrototypeOf(obj) === proto; // true
Copy to Clipboard Non-object coercion In ES5, it will throw a TypeError exception if the obj parameter isn't an object. In ES2015, the parameter will be coerced to an Object.
Object.getPrototypeOf('foo');
// TypeError: "foo" is not an object (ES5 code)
Object.getPrototypeOf('foo');
// String.prototype (ES2015 code)