Explication code

Dédé86 Messages postés 682 Date d'inscription vendredi 23 décembre 2005 Statut Membre Dernière intervention 14 mars 2017 - Modifié par Dédé86 le 14/03/2017 à 09:34
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 - 20 mars 2017 à 18:01
Bonjour,

Je rencontre un problème avec mon code Javascript / Jquery.

Je n'arrive pas à comprendre pourquoi ça ne fonctionne pas.

Voici mon code :

function Gallery(idGallery){

this.idGallery = idGallery;
/*idGallery vaut 12*/
console.log("valeur idGallery"+idGallery);

this.loadImg = function(){
fullUrl = constantes("ajax")+"?fonction=pageGallery&idGal="+this.idGallery;
this.idGallery = 8;
/*idGallery vaut 8*/
console.log("valeur idGallery dans loadImg "+this.idGallery);
that = this;
$.getJSON(fullUrl).done(this.loadImageCallback);
/*this.idGallery vaut 8, pourquoi je ne récupère pas la valeur modifié dans loadImageCallback (6), je croyais
  • la variable that contenait la référence de mon instance de this et donc que le récupérerai ma valeur modifié */ console.log("valeur this.idGallery après callback => "+this.idGallery); this.loadImageCallback = function(data){ /*idGallery vaut 8*/ console.log("valeur de that.idGallery "+that.idGallery); that.idGallery = 6; /*idGallery vaut bien 6*/ console.log("changement valeur pour stocker 6 ->"+ that.idGallery); } } aGallery = new Gallery(12);



Donc comme indiqué, je ne comprends pas pourquoi je ne récupère pas la valeur que j'ai modifié dans loadImageCallback

Je m'excuse je n'arrive pas remettre l'indentation proprement, j'espère que vous me pardonnerez, sachant que le code n'est pas très très long.
Merci beaucoup d'avance pour vos lumières ;-)

1 réponse

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
20 mars 2017 à 18:01
var __galleryIntances = 0;

var Gallery = function ()
{
  console.log( 'Create new Gallery' );
  console.log( '  Begin' );
  
  this._name = 'gallery'+ (++__galleryIntances);
  console.log( '    Gallery name is ' + this._name);
  
  this.id    = arguments.length>=1 ? arguments[0] : 1;
  this._idBG = arguments.length>=2 ? arguments[1] : 8;
  this._idCB = arguments.length>=3 ? arguments[2] : 6;
  console.log( '    Gallery indexes is ' + this.id + ' / ' + this._idBG + ' / ' + this._idCB);
 
  this.log = function(aMessage)
  {
    console.log(this._name + ' : ' + aMessage );
    
    return this;
  };
  console.log( '    Gallery log function created');
  
  this.callBack = function()
  {
    this.log( 'Calling callBack func' ); 

    this.id = this._idCB; 
    this.log( this.id );
  };
  console.log( '    Gallery callBack function created');
  
  this.load = function()
  {
    this.log( 'Calling load func' ); 
    
    fullUrl = "https://code.jquery.com/jquery-3.2.0.slim.min.js"; 
        
    this.id = this._idBG;
    
    $.getJSON( fullUrl, this.callBack() );
      
    return this;
  };
  console.log( '    Gallery load function created');
  console.log( '  End' );
} 

g1 = new Gallery(12); 
g2 = new Gallery(10,3,5); 
g3 = new Gallery(11,0,0); 

g1.load();
g2.load();
g3.load();
1
Rejoignez-nous