0

Object.createドキュメントを読んだ後。私はいくつかのテストを行いました。これが私のコードです。見直してください。

function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

Rectangle=Object.create(Shape);

Rectangle.move(); //?? why move function is not found ?

ドキュメントが言うObject.create(proto[,propertiesObject]); protoように、新しく作成されたオブジェクトのプロトタイプである必要があります。したがって、Rectangle.prototypeと等しいはずShapeです。しかし、実際にはそうではありません。どうやら私はドキュメントのこの部分を理解していませんでした。そして、私はまだRectangle.__proto__==Shape真実であることを発見しました。OK, でもRectangle.__proto__==Shapetrueです ,関数Rectangleが見つからないのはなぜですか? move関数はmoveプロトタイプチェーンにありませんか?? move関数は にあると思ってRectangle.__proto__.prototypeいましたが、チェーンにあるはずです。なぜできません?ありがとう。

4

2 に答える 2

3

プロトタイプは実際のオブジェクトでなければなりません。この場合、Shape 関数ではなく、Shape のプロトタイプを渡す必要があります。

function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

Rectangle=Object.create(Shape.prototype, {a:1});

Rectangle.move(); // it will call now
Rectangle.a; // 1
Rectangle.x; // NaN ???
Rectangle.y; // NaN ???

Object.create()キーワードを使用するのと同じではないことに注意してくださいnew- 代わりに探していたものかもしれません。

function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

Rectangle=new Shape;

Rectangle.move(1,2); // works properly now
Rectangle.a; // undefined, we never made one
Rectangle.x; // 1
Rectangle.y; // 2

Javascript は実際にコンストラクター.prototypeを検索し、再帰的にプロトタイプを見つけるため、直接設定されておらず、new作成にコンストラクターが使用されていないため、Shape のプロトタイプを検索しませんRectangle

function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

Rectangle = Object.create(Shape);
Rectangle.constructor; // Function()
Rectangle.constructor.prototype; // That's Function.prototype
/* as you can see Shape.prototype is never touched by the prototype chain */

Rectangle.__proto__; // Shape(), not the prototype (doesn't have any direct properties on it)

Rectangle.move(1,2); // TypeError: Rectangle.move is not a function
Rectangle.a; // does not exist
Rectangle.x; // function never called on Rectangle, so also doesn't exist
Rectangle.y; // function never called on Rectangle, so also doesn't exist
于 2013-04-24T15:33:11.453 に答える
2

多分これはあなたがもう少し理解するのを助けるでしょう:

https://www.youtube.com/watch?v=DwYPG6vreJg&feature=player_detailpage#t=739s

ここで彼は、あなたが言ったのと同じようには機能しないと説明しています。あなたの主張

私はmove機能がRectangle.__proto__.prototype

正しい。moveとして見つけることができますが、として見つけることができるという意味Rectangle.__proto__.prototype.moveはありませんRectangle.move。プロトタイプ チェーンが中断されます。動画で詳しく説明されていると思います。

コードの次の部分について考えてみてください。

function Shape() {
    this.x = 0;
    this.y = 0;
}

Shape.__proto__.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

Rectangle=Object.create(Shape);

Rectangle.move();

また:

function Shape() {
    this.x = 0;
    this.y = 0;
}

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

Rectangle=Object.create(Shape);

Rectangle.prototype.move();

(これらの場合、x と y はまだ正しくありませんが、それらについて質問していませんでした;))

于 2013-04-29T17:34:10.387 に答える