1
<script>
function Person(name) {
  this.name = name;
}
Person.prototype.kind = 'person'
var zack = new Person('Zack');
console.log(zack.__proto__ == Person.prototype); //=> true
console.log(zack.__proto__ == zack.prototype) //=> false
</script>

Question:

why this line: console.log(zack.__proto__ == zack.prototype) shows false? I checked online about the difference between __proto__ and prototype, but it is quite complex, still do not understand. Anyone can give me a simple and clear explantion? Thanks.

user2294256
  • 1,029
  • 1
  • 13
  • 22
  • See also: http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype – StuartLC Jun 18 '13 at 08:22

1 Answers1

4

Because zack.__proto__ is zack.constructor.prototype, and zack.constructor is Person, and zack has no property named prototype defined.

If you console.log(zack.prototype) you will see it is undefined!

So this is what is happening:

console.log(zack.__proto__ == Person.prototype); //=> true

zack.__proto__ is Person.prototype.

console.log(zack.__proto__ == zack.prototype) //=> false

zack.__proto__, being Person.prototype, can't be an undefined property.

If you want to access prototype of Person from the instantiated variables, you can do a little trick which I don't recommend, because it is recursive and succeed in an infinite loop of properties.

Person.prototype.prototype = Person.prototype;
Niccolò Campolungo
  • 11,824
  • 4
  • 32
  • 39