3

__proto__「はオブジェクトの内部プロパティであり、そのプロトタイプを指している」と理解しているので、次の例では、これc2.prototypeはに等しいと思いますc2.__proto__。なぜそれらは同じ値を持たないのですか?

<!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>
4

3 に答える 3

3

簡単な答えは、c2.constructor.prototype == c2.__proto__

コンストラクターには.prototypeプロパティがあります。インスタンスにはありませんが.__proto__.constructorプロパティはあります

于 2012-06-14T22:12:42.530 に答える
2

obj.__proto__は の短縮形でありobj.constructor.prototype、 のではありませんobj.prototype:

console.log(c2.constructor.prototype === c2.__proto__);   //true
console.log(c2.prototype === c2.__proto__);   //false
于 2012-06-07T08:01:24.910 に答える
1

以下を試してください。

console.log(c2.constructor.prototype);
console.log(c2.__proto__);

実際、c2 がオブジェクトの場合は.__proto__==です。.constructor.prototype

于 2012-06-07T07:59:41.537 に答える