bug, undefined global variable for javascript var

This is the case:

  1. Create an application, lets say a control or grid application.
  2. Go to onScriptInit event and put the following code:

?>
<script type="text/javascript">
    
    var letterIndex = 0;
    var competenceLetter = ["A", "B", "C", "D", "E"];
    
    function letter(){
        var letra = '';
          if (letterIndex == 5){
            letterIndex = 0;
          }
          letra = competenceLetter[letterIndex];
          letterIndex ++;
          return letra;
    }
</script>
<?php

When you run the application, the following error is displayed: “undefined global variable: competenceLetter;”

Debugging the “error” I have found that this line letra = competenceLetter[letterIndex] is the root cause. ScriptCase thinks that letterIndex var is not initialized at the moment of passing it as an index in the array. The code is actually correct, works in blank application and standalone php/javascript project.

Other references:
Looks like this bug has been reported before at 2012, not use if something was done at that moment.

I would have expeceted SC to (wrongly!) throw an error for the [letterindex] var.

Anayway, if the SC parser is the culprit you should escape the [ ] so that SC doesn’t think it’s a global var.

No idea how to escape for the SC parser or if it’s even possible.
Usually I end up writing horrible things like

echo “myvar = myarr[”.“arrindex”."];";

Got it…
I can’t believe it, this is completely pathetic.
Thank you.

PS:

This also apply for php flat arrays like:


$names = [
    "foo", "Bar"
];