If you want to add to the prototype of the object john
refers to, doing so through john.__proto__
gives the impression to the reader of the code that this change is specific to john
somehow, which it isn't; it affects all objects that use that prototype (current and future). Using Person.prototype
doesn't give that impression, it correctly indicates what you're doing.
Separately, note that not all objects (not even all objects with prototypes) have __proto__
, because __proto__
is inherited from Object.prototype
, but not all objects inherit from Object.prototype
. For that reason, in new code, it's best to avoid __proto__
entirely; use Object.getPrototypeOf
instead if you need to get the prototype of an object: Object.getPrototypeOf(john)
.
In a comment you've answered the question of why you'd want to do this with:
To add methods such that they are not present in every Person object you create.
...but that's exactly what adding to john.__proto__
would do: Add the method to every Person
object (indirectly through the prototype), current and future, exactly like adding to Person.prototype
— because those (and Object.getPrototypeOf(john)
) are all just different ways of getting to the same object:
function Person(name) {
this.name = name;
}
var john = new Person("John");
console.log(john.__proto__ === Person.prototype); // true
console.log(Object.getPrototypeOf(john) === Person.prototype); // true
var paul = new Person("Paul");
john.__proto__.someNewMethod = function() {
console.log(this.name);
};
john.someNewMethod(); // "John"
paul.someNewMethod(); // "Paul"
If you want to add a method just to the object john
refers to, don't use __proto__
or Person.prototype
at all: Just do:
john.niftyMethod = function() { /*....*/ };