Variables globales en JS

Fermé
hello - 7 juil. 2014 à 09:05
 Utilisateur anonyme - 7 juil. 2014 à 11:25
Bonjour,
Je déclare une variable globale dans mon programme, je l'utilise dans des fonctions, et à la sortie de ces fonctions la variable globale est vide et n'a pas changée.
Je vous mets mon code : c'est simplement un parser de fichier csv. Ma variable globale s'appelle "Mytree", et regardez à la fin du code, je fais un affichage avant la fin de la fonction anonyme et Mytree est bine remplis et après la variable est vide (cf ci dessous).
Ma question : pourquoi ? Et comment faire pour que ma variable garde son contenu.

// Le programme :
var Mytree = {};
d3.csv("data.csv", function(data) {

/* -- Parsing du csv -- */
var dataTab = [];
var j = 0;
var exigence ="";
var root = "Systeme";

for (var i = 0 ; i < data.length ; i++){
if (data[i].Contrat !== ""){
if(data[i].ExigenceContractuelle.split(".").length !== 1){
exigence = data[i].ExigenceContractuelle.split(".")[0];
if (exigence.split("_").length !== 1){
exigence = exigence.split("_")[0];
}
}
else if (data[i].ExigenceContractuelle.split("-").length !== 1){
exigence = data[i].ExigenceContractuelle.split("-")[0];
}
else if (data[i].ExigenceContractuelle.split("_").lenght !== 1){
exigence = data[i].ExigenceContractuelle.split("_")[0];
}
else {
exigence = data[i].ExigenceContractuelle.split(".")[0];
}

dataTab [j] = {name:data[i].ExigenceSystème, textExig : data[i].TexteExigence,
validation : data[i].Validation, discution : data[i].Discution,
SSSExig : data[i].LibelleTexte, team: root+"/"+exigence+"/"+data[i].ExigenceContractuelle};
j++;
}
}
/* -- -- */


/* -- Tableau -> Json Tree -- */

function fillTree(name, textExigTmp, SSSExigTmp, validation, discution, steps) {
var current = null,
existing = null,
i = 0;
for (var y = 0; y < steps.length; y++) {
if (y===0) {
if (!Mytree.children||typeof Mytree.children === 'undefined'){
Mytree = { text: steps[y], textExig: textExigTmp, SSSExig: SSSExigTmp, leaf: false, children: [], open:false, discution: discution =="" ? 0 : discution, validation:validation==1?true:false };
}
current = Mytree.children;
} else {
existing = null;
for (i=0; i < current.length; i++) {
if (current[i].text === steps[y]) {
existing = current[i];
break;
}
}
if (existing) {
current = existing.children;
} else {
current.push({ text: steps[y], textExig: textExigTmp, SSSExig: SSSExigTmp, leaf: false, children: [], open: false, discution: discution =="" ? 0 : discution,validation:validation==1?true:false });
current = current[current.length - 1].children;
}
}
}
current.push({ text: name, textExig: textExigTmp, SSSExig: SSSExigTmp, leaf: true, open: false, discution: discution =="" ? 0 : discution,validation:validation==1?true:false });
}

for (x=0; x < dataTab.length; x++) {
steps =dataTab[x].team.split('/');
fillTree(dataTab[x].name, dataTab[x].textExig, dataTab[x].SSSExig, dataTab[x].validation, dataTab[x].discution, steps);
}
/* -- -- */
console.log(Mytree);
}); console.log (Mytree);


/* Premier affichage :
Object { text: "Systeme", textExig: "ARCH.01 The VCS SHALL be integrated in the existing ATS Ground Voice Network (AGVN) infrastructure. This implies that the interfaces and components that are provided as part of the VCS, and the number of VCS instances SHALL comply with the architecture as depicted in the figure below. For DSNA and MUAC figure 1 applies, for MUAC figures 2 and 3 apply as well.
Note 1:Figure 1 depicts the overall architecture, and the place of the VCS therein. The high level architecture identifies multiple 'voice systems'. The distinction is based on the way the system is used (online versus test/training) as well as the operational conditions under which the system is used (primary, secondary, fallback or last resort). For some ASNPs, 'fallback' and 'last resort' may be equivalent. A voice system internally consists of multiple (sub) systems, each providing a well-defined service. The subsystem that provides the actual voice communication services is the VCS (sub)system.
Figure 1 : high level architecture
- MUAC SPECIFIC - Figure 2 : Internal architecture ONL Voice systems, internal structure VCS-related tools in SEE environment, and system internal (green) and system external (red) interfaces.
Note: This figure only applies for MUAC. Technical Surveillance Complex(TSC) and Recording and Logging Infrastructure (RLI) are MUAC-specific subsystems within each Voice System. Super Role Allocation Tool (SRAT) interfaces with the VCMS Role Allocation Functionality. The interfaces to TSC, RLI and SRAT are mentioned in the section 'Additional MUAC requirements', and are fully detailed in Chapter 9 (Management System) Note: For other ASNPs, components in the Backup Voice System may also be indicated in dark blue colour, depending on ARCH.02
MUAC SPECIFIC - Figure 3 : internal structure TTI Voice systems, internal structure VCS-related tools in SEE environment, and system internal (green) and system external (red) interfaces
Note: This figure only applies for MUAC. Technical Surveillance Complex(TSC) and Recording and Logging Infrastructure (RLI) are MUAC-specific subsystems within each Voice System. Super Role Allocation Tool (SRAT) interfaces with the VCMS Role Allocation Functionality. The interfaces to TSC, RLI and SRAT are mentioned in the section 'Additional MUAC requirements', and are fully detailed in Chapter 9 (Management System) Note: For other ASNPs, components in the Backup Voice System may also be indicated in dark blue colour, depending on ARCH.02", SSSExig: "Exigences ARCH à dériver dans le DD CRNA
", leaf: false, children: Array[18], open: false, discution: "1", validation: true }

Second affichage :
Object { depth: 0, value: 0, x: 0, y: 0, dx: 550, dy: 550, area: 0 }
*/


1 réponse

Utilisateur anonyme
7 juil. 2014 à 11:25
Bonjour

Ben non, ta variable n'est pas vide lors de ton second affichage. Même si son contenu est très différent de celui du premier affichage.
Si je comprends ton code, ton premier affichage a lieu pendant l'appel d'une fonction d3.csv. Mais que fait cette fonction ? Car selon ce qu'elle fait, il peut se passer plein de choses entre tes deux affichages.
0