Ok après un jour j'ai réussi à affiner le problème à 2 lignes de code. Peut-être que j'essaie d'utiliser incorrectement cette déclaration.gratter ma tête sur "cette" déclaration en javascript. n'importe qui aide s'il vous plaît
function scheduleItemView(myId){
this.update = function(show){
document.getElementById(this.id+'-title').innerHTML = show.title +": "+ show.startDate;
document.getElementById(this.id+'-title-overlay').innerHTML = show.title +": "+ show.startDate;
document.getElementById(this.id+'-description').innerHTML = truncate(show.description,190);
document.getElementById(this.id+'-time-start').innerHTML = show.startTime;
document.getElementById(this.id+'-time-end').innerHTML = show.endTime;
};
this.id=myId;
return true;
}
function nowNextView(){
this.now = new scheduleItemView('now');
this.next = new scheduleItemView('next');
this.update = function(type,args){
var myshow=args[0];
// problem is below. I have to use the global name to access the update method.
myNowNextView.now.update(myshow.now);
myNowNextView.next.update(myshow.next);
// whereas what I want to do is reference them using the "this" command like below.
// this.now.update(myshow.now);
// this.next.update(myshow.next);
// the above doesnt work. The update method in scheduleItemView is not seen unless referenced globally
// BUT even more infuriating, this.now.id does return "now" so it can access the object, just not the method
// any ideas?
};
}
objet
est alors instancié avec
var myNowNextView = new nowNextView();
puis je lance la méthode:
myNowNextView.update(stuff);
J'ai essayé de décrire le problème dans le corps du programme. Aucune erreur dans le code n'a été levée, et j'ai dû faire un essai avant de me dire à contrecoeur qu'il ne pouvait pas trouver la méthode. La conception est-elle imparfaite? Est-ce que je ne peux pas faire ça? Un grand merci à l'avance, Steve
En fait, il semble que cela devrait fonctionner. Si vous appelez 'myNowNextView.update()', alors 'this' dans' update' se référera à 'myNowNextView'. –