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

  1. #1
    Débutant  
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 096
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 096
    Points : 944
    Points
    944
    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
    Débutant  
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 096
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 096
    Points : 944
    Points
    944
    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