別のオブジェクトのコンストラクターの「this」割り当てを使用する JavaScript オブジェクトを取得しようとしているだけでなく、そのすべてのオブジェクトのプロトタイプ関数を想定しています。これが私が達成しようとしているものの例です:
/* The base - contains assignments to 'this', and prototype functions
*/
function ObjX(a,b) {
this.$a = a;
this.$b = b;
}
ObjX.prototype.getB() {
return this.$b;
}
function ObjY(a,b,c) {
// here's what I'm thinking should work:
this = ObjX(a, b * 12);
/* and by 'work' I mean ObjY should have the following properties:
* ObjY.$a == a, ObjY.$b == b * 12,
* and ObjY.getB == ObjX.prototype.getB
* ... unfortunately I get the error:
* Uncaught ReferenceError: Invalid left-hand side in assignment
*/
this.$c = c; // just to further distinguish ObjY from ObjX.
}
ObjY が ObjX の「this」への割り当てを包含し (つまりthis.$* = *
、ObjY のコンストラクターですべての割り当てを繰り返す必要がない)、ObjY が ObjX.prototype を想定する方法について、ご意見をお聞かせください。
私の最初の考えは、次のことを試すことです。
function ObjY(a,b,c) {
this.prototype = new ObjX(a,b*12);
}
理想的には、プロトタイプの方法でこれを行う方法を学びたいと思います (つまり、Base2のような「古典的な」OOP 代替を使用する必要はありません)。
ObjY が匿名 (例: factory['ObjX'] = function(a,b,c) { this = ObjX(a,b*12); ... }
) であることは注目に値するかもしれません。
ありがとうございました。