I am trying to assign a variable to a parameter obtained by the function:

function listResults(entries) { var gStr = ''; var file_i = 0; var dir_i = 0; entries.forEach(function(entry) { if (entry.isDirectory) { //gStr += 'dir_' + dir_i + '=' + entry.name + " "; //dir_i++; } else { /***last modificated date*/ var lastFileModDate = function(metadata) { //console.log("Last date: " + metadata.modificationTime); modificationTimes += metadata.modificationTime; } /**file size*/ var fileSize = function(file) { //console.log("File size: " + file.size); fileSizes += file.size; } modificationTimes = entry.getMetadata(lastFileModDate, fail); fileSizes = entry.file(fileSize, fail); gStr += 'file_' + file_i + '=' + entry.name + ' date_file_' + file_i + '=' + modificationTimes + ' file_size_' + file_i + '=' + fileSizes + " | "; file_i++; } }); } 

she again becomes undefined . What can be done so that it does not lose data?

Here is even an example of the output displays undefined http://jsfiddle.net/vL5nF/ why so?

  var a; var p = function(m) { a = m + 2; } document.write(p(3)); 

For those who are too lazy to follow the link

Closed due to the fact that the participants are off- topic Athari , Sergey , DeKaNszn , user31688, PashaPash 3 May '15 at 20:39 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Athari, Sergey, DeKaNszn, Community Spirit, PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    Again you ^ _ ^ You are not tired of suffering? Read at least JS Good Parts and watch Crockford's video. You see what does not add up with you with JS - Zowie
  • one
    Yes, @AlexWindHope, nowhere else without it ... - AseN pm
  • 2
    @dajver, and what's the 'date_file_' + file_i + '=' + modificationTime + Where does this piece return the result and why ^ _ ^ - Rules
  • The code is fixed, and from the tips I did not understand how I could solve this problem. Anyway, when I write modificationTimes + = metadata.modificationTime; I also equal undefined - dajver
  • 3
    > even gave an example and what did you expect from the function without return 'a? return a = m + 2; and you will have a normal result, but when you already understand that simply asking unintelligible questions on this or any other resource, you cannot understand what is happening in your scripts - Specter

3 answers 3

Go to javascript.ru and get at least the basics of JavaScript
Your question is not at all correct (I wonder how it hasn’t been mentioned before ...)
And about the code so all variables will obey the usual laws of the PLO:

 var modificationTime = 0; function lastFileModDate(metadata) { return metadata * 5; } alert('modificationTime: ' + modificationTime + '\n' + 'lastFileModDate: ' + lastFileModDate + '\n' + 'lastFileModDate(5): ' + lastFileModDate(5) + '\n'); 

PS For example, each function (it has an object) has a toString () method that is OOP:

 function a(b){return b*5;} // a - это обьект alert(a); a = function(b){ return b*5; } a.toString = function(){return "Секрет..."}; // вот доказательство alert(a); alert(a(5)); 
  • four
    OOP laws? - Specter
  • 3
    OOP laws even @ Kotick did not hear> JS without a call operator ("()") for all objects trying to call toString () var a = function () {...}; var b = function (c) {...}; b (a); Is there really a.toString() in the c variable? probably because of such free interpretations of js behavior, books by Russian authors are not recommended for reading - Specter
  • five
    I do not consider "javascript.ru" for mega-resources when there are more worthy English-language resources on the network. No need to impose where to scoop information. And if the function without the return statement returns undefined, then you should not call it null for null! == undefined (as you yourself noticed). The fact that such a comparison of null == undefined will result in a type conversion does not give the right to write erroneous comments accusing the rest of not knowing JS :) - iKuzko
  • 3
    @Spectre * I went on an urgent basis to read about the laws of the PLO * - Costantino Rupert
  • 3
    The visibility of variables is somehow independent of the laws of Object Oriented Programming, or of the laws of the Organization for the Liberation of Palestine. - alexlz

All functions create their own namespaces.

You declared a global variableTimeTime.

Then, in the function, we assigned a value to it and at this stage in the function you have one value of this variable (which you assigned), and the original function remained outside the function - in this case, undefined .

Then you try to work with a global variable in another function and there it is, of course, undefined .

UPD: As @Yura Ivanov correctly noted, all of the above is wrong (with the exception of the first paragraph).

The variant at which modificationTime remains undefined all 2 see:

  1. You declared a variable inside the function and changed it there, and later tried to use it outside the scope ( see @Yura Ivanov as an example below).
  2. entry.getMetadata(); called earlier than lastFileModDate() .

Try changing "var modificationTime;" on "var modificationTime=0;" and do the same. If 0 is inserted, then you do not change the value of this variable. If it is still undefined , then this function is not seen by the entry.getMetadata() function.

  • one
    those. Do you think that changing the value of a global variable inside a function is impossible? This is a false statement. Now, if another variable with the same name were declared in the function, then changing it, yes, would not affect the global variable. most likely the error occurred approximately because of this code: function foo () {var a = 0; function foobar () {a ++;} foobar (); // a == 1} bar (a); // a - undefined - Yura Ivanov
  • one
    I completely agree, you can change the value of a variable from a function. The time was later and I confused myself :) From the given example it is difficult to understand what, where and how exactly it is initialized and called. - iKuzko
  • modificationTime returns 0 when modificationTime = 0; - dajver
  • one
    In this case, the lastFileModDate function is not executed. entry.getMetadata (lastFileModDate, fail); - here you pass the function to getMetadata, and not the result of its execution. Perhaps this will help here: entry.getMetadata (lastFileModDate (), fail); Although it is better to add an explicit return value in lastFileModDate. But in general it is difficult to understand the problem from the scarce data from the question. More code is needed for analysis :) As @Rules noted, it’s not clear where this piece of code '' date_file_ '+ file_i +' = '+ modificationTime + " comes from - iKuzko
  • corrected the code for full, gStr is a variable into which I load all the data and send it to the server. - dajver

For those who are too lazy to follow the link

Those who are too lazy to follow the link answer that they are too lazy to think a little. Not in the return function — so keep your undefined .