11548 sujets

JavaScript, DOM et API Web HTML5

Bonjour,

depuis quelques jours, j'essaie de faire de l'héritage avec Javascript et surtout d'appeler le constructeur de la super-classe mais en vain.
En fait, j'aimerais faire hériter la classe GMap2 (Google Maps) mais ça plante à chaque fois.
Voici mon bout de code :


Map.prototype = new GMap2;
Map.prototype.constructor = Map;

/**
 * The Map constructor
 *
 * @param (String) container
 * 		The container's id
 */
function Map(container) {
	// Creation of the Google Map in the container
	var cont = document.getElementById(container);
	GMap2.call(this, cont);
	// Addition of several controls
	this.addControl(new GLargeMapControl());
	this.addControl(new GMapTypeControl());
	this.addControl(new GScaleControl());
	this.addControl(new GOverviewMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
	this.setMapType(this.getMapTypes()[MAP_START_TYPE]);
	// Centering of the map
	this.setCenter(new GLatLng(MAP_START_LAT, MAP_START_LON), parseInt(MAP_START_ZOOM));
}


Que pensez-vous de ça ? Vous voyez l'erreur ???

Merci en tout cas si vous arrivez à m'aider Smiley cligne

Cordialement,
Flavien
Modifié par Flavien (10 Jul 2006 - 09:27)
Je pense que pour que cela fonctionne mieu il faut déclarer le contructeur "Map" avant le prototypage :
[#green]/**

 * The Map constructor

 *

 * @param (String) container

 * 		The container's id

 */[/#]

[#brown]function Map(container) {

	// Creation of the Google Map in the container

	var cont = document.getElementById(container);

	GMap2.call(this, cont);

	// Addition of several controls

	this.addControl(new GLargeMapControl());

	this.addControl(new GMapTypeControl());

	this.addControl(new GScaleControl());

	this.addControl(new GOverviewMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));

	this.setMapType(this.getMapTypes()[MAP_START_TYPE]);

	// Centering of the map

	this.setCenter(new GLatLng(MAP_START_LAT, MAP_START_LON), parseInt(MAP_START_ZOOM));

}[/#]
[#darkblue]Map.prototype = new GMap2;[/#]

En plus je pense que "Map.prototype.constructor = Map;" ne sert à rien (le constructeur est déja déclaré) et en plus j'ai l'impression que la méthode constructor ne change rien quand elle est redéfinit...
heyman85 a écrit :
Je pense que pour que cela fonctionne mieu il faut déclarer le contructeur "Map" avant le prototypage :
[#green]/**

 * The Map constructor

 *

 * @param (String) container

 * 		The container's id

 */[/#]

[#brown]function Map(container) {

	// Creation of the Google Map in the container

	var cont = document.getElementById(container);

	GMap2.call(this, cont);

	// Addition of several controls

	this.addControl(new GLargeMapControl());

	this.addControl(new GMapTypeControl());

	this.addControl(new GScaleControl());

	this.addControl(new GOverviewMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));

	this.setMapType(this.getMapTypes()[MAP_START_TYPE]);

	// Centering of the map

	this.setCenter(new GLatLng(MAP_START_LAT, MAP_START_LON), parseInt(MAP_START_ZOOM));

}[/#]
[#darkblue]Map.prototype = new GMap2;[/#]

En plus je pense que "Map.prototype.constructor = Map;" ne sert à rien (le constructeur est déja déclaré) et en plus j'ai l'impression que la méthode constructor ne change rien quand elle est redéfinit...


Bonjour,

bon en fin de compte, j'ai réutilisé un bout de code que j'avais déjà utilisé (je ne me souvenais plus de son existence Smiley langue ) :

extend = function(subClass, baseClass) {
   function inheritance() {}
   inheritance.prototype = baseClass.prototype;
   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}

Merci pour votre aide.

Cordialement,
Flavien