IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Port�e des variables


Sujet :

JavaScript

Vue hybride

Message pr�c�dent Message pr�c�dent   Message suivant Message suivant
  1. #1
    Membre exp�riment�
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 167
    D�tails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 167
    Par d�faut Port�e des variables
    Bonjour,

    J'ai cru avoir appris qu'une variable d�clar� avec let est locale, et celle cr��e avec var est global.

    J'ai une condition if, dans laquelle j'ai cr�� une variable let pour faire un calcul
    Code javascript : S�lectionner tout - Visualiser dans une fen�tre � part
    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
    if($(this).hasClass("hasChildren")){
     
    			$(this).attr("style","border:2px solid blue !important");
     
    			let a = $(this).first().children('div').first().children('a:first-child').attr("style","border:1px solid yellow !important");
     
     
    		}
    		else{
    			let a = $(this).first().children('div').first().children('a').attr("style","border:10px solid white !important");
     
    			let a_padding = Math.ceil((a.outerWidth() -  a.width()) - (a.outerWidth() - a.innerWidth()));
    			let a_border = Math.ceil(a.outerWidth() - a.innerWidth());
    			let a_wwidth = Math.ceil(a.width());
    			let a_paddingBorder = a_padding +  a_border;
    			let a_width = a_wwidth + a_border;
     
    			console.log("a_padding:", a_padding);
    			console.log("a_border:", a_border);
    			console.log("a_widthWithoutBorder:", a_wwidth);
    			console.log("div_paddingBorder:", a_paddingBorder);
    			console.log("a_width:", a_width);
    			console.log("----------");
    		}
     
    console.log(a_width);

    Dans ce cas, mon console.log ne va rien afficher, alors que console.log("a_width:", a_width); va afficher un r�sultat

    Donc j'ai modifi� comme cela
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    		var a_width =1;
     
    		if($(this).hasClass("hasChildren")){
     
    			$(this).attr("style","border:2px solid blue !important");
     
    			let a = $(this).first().children('div').first().children('a:first-child').attr("style","border:1px solid yellow !important");
     
     
    		}
    		else{
    			let a = $(this).first().children('div').first().children('a').attr("style","border:10px solid white !important");
     
    			let a_padding = Math.ceil((a.outerWidth() -  a.width()) - (a.outerWidth() - a.innerWidth()));
    			let a_border = Math.ceil(a.outerWidth() - a.innerWidth());
    			let a_wwidth = Math.ceil(a.width());
    			let a_paddingBorder = a_padding +  a_border;
    			a_width = a_wwidth + a_border;
     
    			console.log("a_padding:", a_padding);
    			console.log("a_border:", a_border);
    			console.log("a_widthWithoutBorder:", a_wwidth);
    			console.log("div_paddingBorder:", a_paddingBorder);
    			console.log("a_width:", a_width);
    			console.log("----------");
    		}
     
     
     
    		 menu[index] = {
    		  	index: index,
    		  	div_padding: div_padding,
    		  	div_border: div_border,
    		  	div_paddingBorder: div_paddingBorder,
    			a_width: a_width,
    			w_total: div_paddingBorder + a_width,
    		  	name:"li-"+index
    		  };
    	});
     
    	console.log(menu);
    Pourquoi ceci m'affiche un r�sultat
    console.log("a_width:", a_width); alors que dans console.log(menu); il m'affiche
    a_width: undefined
    Le fait d'avoir fait ceci var a_width =1; n'est-ce pas suffisant pour que la variable soit globale?

    Pierre

  2. #2
    Membre exp�riment�
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 167
    D�tails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 167
    Par d�faut
    OK, je devais la mettre avant $(document).ready(function(){})

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. R�ponses: 8
    Dernier message: 17/02/2005, 09h05
  2. [FLASH MX] Port�e des variables ???
    Par mic79 dans le forum Flash
    R�ponses: 2
    Dernier message: 08/02/2005, 10h21
  3. Port�e des variables vbscript vers ASP
    Par Immobilis dans le forum ASP
    R�ponses: 3
    Dernier message: 03/11/2004, 10h14
  4. [XSL]Probl�me de port�e des variables
    Par djulesp dans le forum XSL/XSLT/XPATH
    R�ponses: 6
    Dernier message: 17/09/2004, 10h34
  5. [Port�e] port�e des variables
    Par parksto dans le forum Langage
    R�ponses: 7
    Dernier message: 09/05/2004, 21h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo