简体   繁体   中英

google apps script loop variable to be declared var?

I am wondering if this is a feature of google apps script (or javascript as a whole for that matter) or a bug. I get a strange result when I call a function from inside a loop. Now, within the called function there is a loop that uses the same variable that the variable running on the calling loop and this seems to cause a problem. Here the code:

 function dudi() {
   var folderName='FormsGenerator';
   var ss=new Array(2);
   for(o=0;o<2;o++){
     var str='dudo' + o;
     trashFile(str,folderName);
     ss[o]=SpreadsheetApp.create(str);
     Logger.log(str);
   }
  }
 function trashFile(fileName,folderName){
   var folder=DocsList.getFolder(folderName);
   var lFolder=folder.getFiles();
   for(o=0;o < lFolder.length;o++){
     if(lFolder[o].getName() == fileName) {
       DocsList.getFileById(lFolder[o].getId()).setTrashed(true);
     }
   }

What happens is that the loop in the calling function stops after the first iteration. If in trashFile I change the loop index variable to "p" or I use a "var o=0" instead of a "o=0", the problem goes away. What am I doing wrong? Is this a well known feature or a bug? I have been programming in C and C++ for long years, but I am fairly new with javascript/GAS.

Max

Now, within the called function there is a loop that uses the same variable that the variable running on the calling loop and this seems to cause a problem.

The reason for the problem is that o isn't declared in either function, so you're falling prey to The Horror of Implicit Globals : In JavaScript, assigning to a variable that doesn't exist creates a global variable. (See below for more.) Since o ends up being a global variable, both functions use the same o , and interfere with each other.

Simply declare o within both functions (eg: var o; ), and the problem will go away, because then each function uses its own local variable rather than a global.

This implicit globals thing is one of the flaws of the original design of JavaScript (all languages have design flaws). They've addressed this one in the new "strict" variant of the language: If you enable strict mode, assigning to a variable that doesn't exist causes an error rather than creating a global.

Examples: (Assume there is no declaration for a anywhere.)

Loose mode:

function foo() {
    a = "bar";    // <=== Creates a global variable called 'a'
}

Strict mode:

function foo() {
    "use strict";

    a = "bar";    // <=== Throws a ReferenceError
}

You can apply strict mode to all of your code by wrapping it a scoping function:

(function() {
    "use strict";

    function foo() {
        a = "bar";    // <=== Throws a ReferenceError
    }
})();

You can also apply it within a script tag by putting it at the top:

<script>
"use strict";
function foo() {
    a = "bar";    // <=== Throws a ReferenceError
}
</script>

This works both with inline script as with the above, and with .js files you load via src= . Beware, though, that if you do this at the top level of a .js file, you have to be careful when combining scripts! (This is one reason I always use scoping functions; the other is that I don't want to create any globals.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM