Skip to content
jshacks.dev
Fundamentals

proto vs prototype vs Object.getPrototypeOf in JavaScript

The prototype property belongs to constructor functions, __proto__ is a legacy accessor for the link, and Object.getPrototypeOf is the supported call.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the JavaScript, Node.js, TypeScript and npm logos.

Three names point at the idea of "the prototype", and they point at different places. Dog.prototype is where instances of Dog get their methods. rex.__proto__ is the link from one instance to that object. Object.getPrototypeOf(rex) is the supported way to read the same link. Mixing them up produces confusing code, such as writing rex.prototype and getting undefined, or assigning Dog.__proto__ when Dog.prototype was meant. The rule that sorts them out is short: prototype is a property of constructor functions, and __proto__ is the link every object carries. This post uses runnable comparisons on Node.js 24 to separate the three, shows where each one is right and where it is a trap, and covers the common mistakes with reassignment and class syntax. The chain itself is drawn in the post on the prototype chain, and the larger topic is in the guide to prototypes and inheritance.

The three names side by side

Set up a constructor, make an instance and compare every pairing.

JavaScript
function Dog(name) {
  this.name = name;
}
Dog.prototype.bark = function () {
  return this.name + " barks";
};

const rex = new Dog("Rex");

console.log(Object.getPrototypeOf(rex) === Dog.prototype);
console.log(rex.__proto__ === Dog.prototype);
console.log(rex.prototype);
console.log(Dog.prototype.constructor === Dog);
console.log(Object.getPrototypeOf(Dog) === Function.prototype);
console.log(Dog.__proto__ === Dog.prototype);
console.log(Object.getPrototypeOf(Dog.prototype) === Object.prototype);
Text
true
true
undefined
true
true
false
true

The first two lines show that __proto__ and Object.getPrototypeOf() read the same link, and that it points at Dog.prototype. The third shows the common mistake: rex.prototype is undefined, because instances do not have a prototype property. The fourth is the back link every prototype has: Dog.prototype.constructor points at the function. The fifth and sixth show that the function Dog is an object with its own prototype link, which goes to Function.prototype, and that is a different object from Dog.prototype.

NameLives onHolds
Fn.prototypeA constructor function or classThe object future instances will link to
obj.__proto__Every object, through Object.prototypeThe object's own prototype link, as a legacy accessor
Object.getPrototypeOf(obj)A static functionThe same link, read the supported way
Object.setPrototypeOf(obj, proto)A static functionChanges the link of an existing object

Where prototype exists and where it does not

Only functions that can be constructors get a prototype property. Arrow functions, methods and async functions do not.

JavaScript
function regular() {}
const arrow = () => {};
const object = {
  method() {},
};
class Box {}

console.log(typeof regular.prototype);
console.log(typeof arrow.prototype);
console.log(typeof object.method.prototype);
console.log(typeof Box.prototype);

try {
  new arrow();
} catch (error) {
  console.log(error.name + ": " + error.message);
}

const descriptor = Object.getOwnPropertyDescriptor(Box, "prototype");
console.log(descriptor.writable, descriptor.enumerable, descriptor.configurable);
Text
object
undefined
undefined
object
TypeError: arrow is not a constructor
false false false

A regular function and a class have one, and an arrow function and a shorthand method do not, which is why neither can be called with new. The prototype property of a class is not writable, so Box.prototype = {} does nothing in a class, though it works on a plain function. That asymmetry is one of the reasons to prefer classes for anything that will be used as a constructor.

__proto__ is a legacy accessor

__proto__ is not a property stored on each object. It is a getter and setter defined once on Object.prototype, and every object inherits it. That explains why an object with no prototype has no __proto__.

JavaScript
const descriptor = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");

console.log(typeof descriptor.get, typeof descriptor.set);

const bare = Object.create(null);
console.log(bare.__proto__);
console.log(Object.getPrototypeOf(bare));

const viaLiteral = { __proto__: { inherited: true } };
const viaSetter = {};
viaSetter.__proto__ = { inherited: true };
const viaApi = Object.setPrototypeOf({}, { inherited: true });

console.log(viaLiteral.inherited, viaSetter.inherited, viaApi.inherited);
Text
function function
undefined
null
true true true

The accessor has both a getter and a setter. The null-prototype object returns undefined for __proto__, since it does not inherit the accessor, while Object.getPrototypeOf() works on it and returns null. All three ways of setting a prototype worked. The literal form { __proto__: value } is part of the language and is fine; the assignment form works through the setter and exists for compatibility. We use Object.create(proto) to create with a prototype, Object.getPrototypeOf() to read, and treat __proto__ as something to avoid in new code. The __proto__ key is also the center of a prototype pollution risk, discussed in the post on objects.

Why Object.setPrototypeOf has a warning

Changing the prototype of an existing object is allowed, and engines optimize object shapes on the assumption that the prototype stays fixed. Replacing it later can deoptimize code that touches the object, which is why the documentation warns against it.

JavaScript
const animal = {
  speak() {
    return "...";
  },
};

const dog = Object.create(animal);
const cat = { __proto__: animal };

const late = {};
Object.setPrototypeOf(late, animal);

console.log(dog.speak(), cat.speak(), late.speak());
console.log(Object.getPrototypeOf(dog) === Object.getPrototypeOf(late));
Text
... ... ...
true

All three ways give the same result, and two of them set the link at creation. Prefer Object.create(proto) and class syntax, which set it once. Reserve setPrototypeOf for rare cases such as a polyfill or an adapter that cannot control creation, and avoid calling it in a hot path.

Reassigning prototype loses constructor

Assigning a fresh object to Fn.prototype is a common style, and it removes the default constructor property that every function's original prototype has.

JavaScript
function Account() {}
Account.prototype = {
  deposit() {
    return "deposit";
  },
};

const first = new Account();

console.log(first.constructor === Account);
console.log(first.constructor === Object);
console.log(first instanceof Account);

function Wallet() {}
Wallet.prototype = {
  constructor: Wallet,
  spend() {
    return "spend";
  },
};

console.log(new Wallet().constructor === Wallet);

function Card() {}
Card.prototype.pay = function () {
  return "pay";
};
console.log(new Card().constructor === Card);
Text
false
true
true
true
true

After the reassignment, first.constructor is Object, because the lookup now goes through the new plain object to Object.prototype. instanceof still works, since it checks the chain and not the property. Adding constructor: Wallet back, or adding methods to the existing prototype with Card.prototype.pay = ..., keeps the link. If code reads instance.constructor to find the class, as some serialization and cloning helpers do, the missing link breaks them.

Classes use the same machinery

A class declaration creates a function with a prototype, and it sets two links when extends is used: one between the prototype objects, and one between the constructors themselves.

JavaScript
class Animal {
  static create() {
    return new this();
  }
  eat() {
    return "eating";
  }
}

class Dog extends Animal {
  bark() {
    return "woof";
  }
}

const rex = Dog.create();

console.log(Object.getPrototypeOf(Dog.prototype) === Animal.prototype);
console.log(Object.getPrototypeOf(Dog) === Animal);
console.log(rex instanceof Dog, rex.eat(), rex.bark());
console.log(Object.keys(Dog.prototype));
Text
true
true
true eating woof
[]

The second link is why static methods are inherited: Dog.create was found by walking from Dog to Animal, and new this() created a Dog. The methods on a class prototype are not enumerable, so Object.keys(Dog.prototype) is empty, unlike the function-style version above. The post on classes being functions goes through the full comparison.

Using the right check for the job

Three checks answer related questions, and picking the right one avoids confusion. instanceof asks whether a constructor's prototype is in the object's chain. isPrototypeOf() asks the same from the prototype's side and works when there is no constructor, as with Object.create. And comparing Object.getPrototypeOf(obj) === Proto asks for an exact match on the immediate link, which is stricter and is the check to use when subclasses must be excluded.

JavaScript
const base = { kind: "base" };
const child = Object.create(base);
const grandchild = Object.create(child);

console.log(base.isPrototypeOf(grandchild));
console.log(Object.getPrototypeOf(grandchild) === base);
console.log(Object.getPrototypeOf(grandchild) === child);

class Shape {}
class Circle extends Shape {}
const circle = new Circle();

console.log(circle instanceof Shape);
console.log(Object.getPrototypeOf(circle) === Shape.prototype);
Text
true
false
true
true
false

The grandchild has base in its chain, though not as its immediate prototype. The class example shows the same split: a Circle is an instance of Shape, and its immediate prototype is Circle.prototype, not Shape.prototype. Use the exact comparison when a function must accept only one specific level, such as a serializer that handles Shape itself and rejects subclasses.

Which to use

For reading, write Object.getPrototypeOf(obj). For creating with a prototype, write Object.create(proto) or a class. For adding shared methods to a constructor function, mutate Fn.prototype. For checking relationships, use instanceof or Proto.isPrototypeOf(obj). And avoid __proto__ in assignments and in code that handles untrusted keys.

Where these names stop mattering

If your code is written entirely with classes and plain objects, you will rarely write any of the three names. They matter when reading older code, writing libraries, debugging an unexpected undefined method and understanding console output, which prints [[Prototype]] for the internal link. Knowing which name is which is mostly a reading skill.

The legacy accessor is standardized in the specification's Annex B as Object.prototype.__proto__, and MDN's page for Object.getPrototypeOf describes the supported function.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.