Prototypes
Every object has a hidden link, called [[Prototype]], to another object or to null. Object.prototype is built in. It holds methods such as toString that ordinary objects share. Its own link is null: the end of the line.
Making objects with a class
A class declaration makes two things: a function called Dog, and an object called Dog.prototype. Methods written in the class body, like speak on line 3, are stored once, on Dog.prototype. Its own link points to Object.prototype.
new Dog("Rex") starts by making a fresh, empty object. It sets that object's hidden link to Dog.prototype. Then it runs the constructor (the class's setup function, line 2), with this pointing at the new object.
this.name = name stores "Rex" in a property called name. It is an own property: it lives on the object itself. new then returns the object, and the name rex now points to it.
new Dog("Fido") repeats the recipe: a new object with its own name, "Fido", linked to the same Dog.prototype. speak is not copied into either dog. Both share the one function.
Looking up a property
rex.speak looks for speak on rex first. rex only has name, so JavaScript follows the hidden link to Dog.prototype. speak is there, so the search stops and that function is called.
speak was called as rex.speak(), so inside it this is rex, the object before the dot. this.name finds "Rex" on rex itself, so speak returns "Rex woofs". console.log prints it.
rex has no toString, and neither has Dog.prototype. JavaScript follows the next link to Object.prototype and finds it there. It prints [object Object]. This series of links is the prototype chain.
No object in the chain has fly. The search reaches null and stops, so rex.fly is undefined, which the console prints. Calling rex.fly() would stop with an error (a TypeError), because undefined is not a function.
A class is a neater way to build prototypes. typeof Dog is "function", and Object.getPrototypeOf(rex) === Dog.prototype is true, and fido is linked the same way. The class sets up the links; reading a property simply walks them.
What happens
// step 1/10 · hidden-link
object a bag of properties (name → value)
[[Prototype]] a hidden link to another object, or null
read it Object.getPrototypeOf(obj)
built in Object.prototype (toString, valueOf, …)
its [[Prototype]] is null
The hidden link is the internal slot [[Prototype]] in the ECMAScript specification. Read it with Object.getPrototypeOf(obj). Browsers also expose it as obj.__proto__, but that is a legacy accessor; prefer getPrototypeOf. The figure shows only the properties the example uses. Dog.prototype also has constructor, which points back to Dog, and Object.prototype has more methods, such as hasOwnProperty and valueOf.