Sitemap
JavaScript Refined

JavaScript internals, news, tools, and opinions

Follow publication

Function and Object, instances of each other

4 min readAug 30, 2016

--

Press enter or click to view image in full size
JavaScript at its finest

Internal [[Prototype]] and external prototype

// Function object that will be used as a constructor
function Car(){}
// Car.prototype has a single property called "constructor"
// which points back to Car
Car.prototype.constructor === Car // true
// Car.prototype is an object
// console.log(Car.prototype) => Object {}
Car.prototype instanceof Object // true
// Create an object using the new keyword
var myCar = new Car()
// The internal [[Prototype]] of `myCar` is initialized
// with `Car.prototype`
Object.getPrototypeOf(myCar) === Car.prototype // true
myCar instanceof Car // true

Function instanceof Object

var internalProto
internalProto = Object.getPrototypeOf(Function)
internalProto === Object.prototype // false
// Since it’s false, move up the prototype chain
internalProto = Object.getPrototypeOf(internalProto)
internalProto === Object.prototype // true
// Since it’s true, no need to move up the chain
Function.prototype // function () {}
Object.getPrototypeOf(Function) // function () {}
// They point to the same function
Object.getPrototypeOf(Function) === Function.prototype // true

Object instanceof Function

var internalProto
internalProto = Object.getPrototypeOf(Object)
internalProto === Function.prototype // true
// Since it’s true, no need to move up the chain

--

--

JavaScript Refined
JavaScript Refined

Published in JavaScript Refined

JavaScript internals, news, tools, and opinions

Kiro Risk
Kiro Risk

Written by Kiro Risk

Author of https://fusejs.io, Engineering @ Instagram, pushing the boundaries of all things tech.

Responses (4)