Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't .__proto__ = .prototype in this example?

Tags:

javascript

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>
like image 813
Edward Tanguay Avatar asked Jan 21 '26 23:01

Edward Tanguay


1 Answers

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

like image 171
Juan Mendes Avatar answered Jan 23 '26 13:01

Juan Mendes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!