Reputation: 193312
I understand that "__proto__
is an internal property of an object, pointing to its prototype" so in the following example I would think that c2.prototype
would equal c2.__proto__
. Why do they not have the same value?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var Circle = function(radius) {
this.radius = radius;
this.doubleRadius = function() {
return this.radius * 2;
}
}
var c1 = new Circle(4);
Circle.prototype.area = function() {
return Math.PI*this.radius*this.radius;
}
var c2 = new Circle(5);
console.log('--- first circle object, created before adding "area" method');
console.log(c1.radius);
console.log(c1.doubleRadius());
console.log(c1.area());
console.log('--- second circle object, created after adding "area" method');
console.log(c2.radius);
console.log(c2.doubleRadius());
console.log(c2.area());
console.log(c2.prototype); // undefined
console.log(c2.__proto__); // Object { area=function() }
}
</script>
</head>
<body>
</body>
</html>
Upvotes: 3
Views: 180
Reputation: 160853
Try the below.
console.log(c2.constructor.prototype);
console.log(c2.__proto__);
Acturly, .__proto__
== .constructor.prototype
when c2 is a object.
Upvotes: 1
Reputation: 92274
The simple answer is that c2.constructor.prototype == c2.__proto__
Constructors have a .prototype
property. Instances don't, but they do have .__proto__
and .constructor
properties
Upvotes: 3
Reputation: 48793
obj.__proto__
is short version of obj.constructor.prototype
, not of obj.prototype
:
console.log(c2.constructor.prototype === c2.__proto__); //true
console.log(c2.prototype === c2.__proto__); //false
Upvotes: 2