J'essaie d'obtenir un objet JavaScript pour utiliser les affectations "this" du constructeur d'un autre objet, ainsi que supposer tous les prototypes de ces objets les fonctions. Voici un exemple de ce que je tente d'accomplir:La fonction Javascript utilisant "this =" donne "Invalid left-side in assignment"
/* 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.
}
je serais reconnaissant de vos réflexions sur la façon d'avoir ObjY subsumer les missions de ObjX à « ce » (c.-à-pas à répéter toutes les this.$* = *
missions en Constructeur de ObjY) et ObjY assume ObjX.prototype.
Ma première pensée est d'essayer ce qui suit:
function ObjY(a,b,c) {
this.prototype = new ObjX(a,b*12);
}
Idéalement, je voudrais savoir comment faire cela d'une manière prototypique (c.-à-pas à utiliser l'un de ces substituts de POO « classiques » comme Base2).
Il est à noter que ObjY sera anonyme (par exemple factory['ObjX'] = function(a,b,c) { this = ObjX(a,b*12); ... }
) - si j'ai la bonne terminologie.
Merci.
liées: [? Pourquoi ne puis-je attribuer une nouvelle valeur à « ce » dans une fonction prototype] (http: // stackoverflow.com/q/9713323/1048572) – Bergi