2

私の状況では、Box2DWeb を使用してキャンバス ゲームに取り組んでいるので、キャンバスに画像を描画するSpriteというクラスがあり、 Spriteからメソッドとプロパティを継承するPhysicsSpriteクラスを作成したいと考えています。

私のスプライトクラス(Sprite.js):

Sprite = function (position, dimension, image) {

  this.Position = position;
  this.Dimension = dimension;
  if (image)
    this.Image = image;
  ...

  this.Draw = function (g) {
    ...
  }
  ...
};

そして、私はPhysicsSprite (PhysicsSprite.js) でプロトタイプを次のように設定しています:

PhysicsSprite = function(position, dimension, world) {

    this.prototype = new Sprite(position, dimension, undefined);
    //alert(this.prototype.Position.X)

    var body = CreateBody();
    this.GetBody = function () { return body; }

    this.Draw = function (g) {
        ...  
    }

    function CreateBody () {
          ...
    }
};

位置を警告するalert(this.prototype.Position.X)に注意してください。ただし、コードをthis.Position.Xに変更するとエラーが発生します。同様に、PhysicsSpriteのインスタンスがある場合は、プロトタイプを明示的に呼び出す必要があります。

これにより、オブジェクトのプロトタイプを設定しておらず、単に というプロパティを作成し、prototypeそれを新しいSpriteに設定しただけだと思いました。

誰かが私を助けて、私が間違っていることを説明できれば、それは大歓迎です。私は失読症なので、いつも変数のスペルを間違えてイライラするので、最初にそれを探しましたが、すべて問題ないように見えました。

4

3 に答える 3

3

これにより、オブジェクトのプロトタイプを設定していない、prototype と呼ばれるプロパティを作成し、それを新しいスプライトに設定しただけだと考えるようになりました。

それは正しい。また、各関数をプロトタイプではなくインスタンス ( ) に直接割り当てるため、関数のプロトタイプをまったく使用していませんthis.Draw = function() ...


JavaScript には、コンストラクター関数と呼ばれるものがあります。演算子で呼び出されるすべての関数newは、コンストラクター関数です。

関数がそのように呼び出されると ( )、によって参照されるオブジェクトから継承new SomeFunc()する新しいオブジェクトが作成され ( isntanceSomeFunc.prototypeと呼びましょう) 、このインスタンスは を介し​​てその関数内で使用できますthis

現在、継承は基本的SomeFunc.prototypeに、(ほぼ)空のオブジェクト(デフォルト)ではなく、別の関数のプロトタイプから継承するプロトタイプ( )を持つことになります。

例:

function A(name) {
    // `this` refers to a new instance
    // lets set some properties:
    this.name = name;
}

// all "class" methods should be assigned to the prototype
A.prototype.getName = function() {
    return this.name;
};


// lets create a child "class"
function B(name, place) {
    // we have to call the parents constructor with the current instance
    // and the arguments we want to pass on.
    // this is like `super(name)` in Java or `A.__init__(self, name)` in Python 
    // (or `super(B, self).__init__(name)` in Python)
    A.call(this, name);
    this.place = place;
}

// here we connect A's prototype with B's prototype in a way that they
// stay independent 
inherits(B, A);

// B's "class" methods
B.prototype.getPlace = function() {
    return this.place;
};

// now we can do
var b = new B('SomeName', 'SomePlace');
alert(b.getName());
alert(b.getPlace());

これはどのようinheritsに見えるかです (この実装は Google Closure ライブラリによって使用されます):

function inherits(Child, Parent) {
    var Tmp = function() {};
    Tmp.prototype = Parent.prototype;
    Child.prototype = new Tmp();
    Child.prototype.constructor = Child;
}

Child.prototypeのインスタンスを参照していることがわかりますTmp。しかし、Tmpのプロトタイプは のプロトタイプと同じParentです。つまり、 によって返されるインスタンスは のプロトタイプからnew Tmp()継承されます。Parentこのインスタンスは のプロトタイプであるChildため、 のすべてのインスタンスもChildから継承さParent.prototypeれます。

ECMAScript 5 では、これを次のように簡略化できます。

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

しかし、それは本質的に同じです。から継承する新しいオブジェクトを作成しますParent.prototype


参考文献:

于 2012-05-06T15:59:23.413 に答える
1

変化する

this.prototype = new Sprite(position, dimension, undefined);

Sprite.apply(this, position, dimension, undefined);

そうです、変更する前に、オブジェクトのインスタンスを「prototype」プロパティに割り当てています。

プロトタイプはコンストラクター関数のプロパティであり、オブジェクトインスタンスではありません。つまり、によってプロトタイプにアクセスできますが、または(where )ではアクセスできPhysicsSprite.prototypeません。this.prototypex.prototypex = new PhysicsSprite()

于 2012-05-06T15:47:01.897 に答える
0

JavaScript は Java とはまったく異なる鳥ですが、両方とも { と } を共有しているため、よく間違えられます。:D

典型的な JavaScript クラス:

function Sprite(a,b) {
    // These are per instance
    this.a = a;
    this.b = b;
}
Sprite.prototype = {
    // These are per class, all instances share them
    Draw: function (g) {
        // ...
    }
};

... そして JavaScript の継承は非常に奇妙です。

于 2012-05-06T15:50:07.903 に答える