[POO] Conception et AddEvent
Bonjour,
Je suis sur un problème plutôt de conception et j'aimerais savoir si vous pourriez m'aider à le résoudre.
J'ai une classe qui créer un objet, jusque là tout va bien.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
/* Constructor */
function OASCaller(pos,lang)
{
//Private vars declaration
this.OASPositions = new String();
this.OASUrl = new String();
this.OASLanguage = new String();
this.OASVersion = new Number();
this.OASSitePage = new String();
this.OASRandomNum = new String (Math.random()).substring (2, 11);
this.OASCreatedObject = new String();
//Private vars default init
this.OASPositions = pos;
this.OASUrl = this.getOASUrl();
this.OASVersion = this.getOASVersion();
this.OASSitePage = this.getOASSitePage();
this.setOASLanguage(lang);
checkPos = this.checkPositions();
if(checkPos)
{
this.InitEvents();
}
} |
J'essaye alors d'implémenter la fonction AddEvent(Listener) afin de charger des fonctions de mon objet à la fin du chargement de la page.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
OASCaller.prototype.InitEvents = function()
{
OASCaller.addEvent(window,'load',this.loadEventsCallBAck);
}
OASCaller.addEvent = function( obj, type, fn )
{
if (obj.addEventListener)
obj.addEventListener( type, fn, false );
else if (obj.attachEvent)
{
obj["e"+type+fn] = fn;
obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
}
} |
Mon souci est que je ne peux pas mettre la référence this dans mon appel de fonction d'objet dans la call back
Code:
1 2 3 4 5 6
|
OASCaller.prototype.loadEventsCallBAck = function()
{
this.setBannerInHtmlElement()
alert(document.getElementById('Right1'));
} |
car this.setBannerInHtmlElement() fonctionne mais me renvoi une erreur d'exécution js dans firebug.
Pour que celà fonctionne je dois appeller cette fonction avec l'instance de mon objet déclaré : myOASCaller.setBannerInHtmlElement().
Hors je ne peux décemment pas utiliser çà en POO.
J'espère avoir bien expliqué mon problème.
En espérant que quelqu'un puisse m'aider.
Merci beaucoup.
Yudao.